mergeProps-composition.uvue 1.4 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<template>
  <view class="page">
    <view class="mb-10 flex flex-row justify-between">
      <text>merged class</text>
      <text id="merged-class">{{mergedProps['class']}}</text>
    </view>
    <button class="mb-10" id="trigger-merged-click" @click="triggerMergedClick">trigger merged onClick</button>
    <view>
      <text class="mb-10">prop function result list</text>
      <text class="click-res" v-for="(item, index) in propFnResList" :key="index">{{item}}</text>
    </view>
  </view>
</template>

<script setup lang="uts">
  type PropFn = () => string

DCloud-WZF's avatar
DCloud-WZF 已提交
18
  // TODO: 确认 android 与 web & ios 差异
DCloud-WZF's avatar
DCloud-WZF 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  // #ifdef APP-ANDROID
  const propA = new Map<string, any | null>([['class', 'foo'], ['onClick', () : string => 'propA click res']])
  const propB = new Map<string, any | null>([['class', { bar: true }], ['onClick', () : string => 'propB click res']])
  const mergedProps = ref(new Map<string, any | null>())
  // #endif
  // #ifndef APP-ANDROID
  const propA = {
    class: 'foo',
    onClick: () : string => 'propA click res'
  }
  const propB = {
    class: { bar: true },
    onClick: () : string => 'propB click res'
  }
  const mergedProps = ref({})
  // #endif
  const propFnResList = ref<string[]>([])

DCloud-WZF's avatar
DCloud-WZF 已提交
37
  mergedProps.value = mergeProps(propA, propB)
DCloud-WZF's avatar
DCloud-WZF 已提交
38 39 40 41 42
  
  const triggerMergedClick = () => {
    (mergedProps.value['onClick'] as PropFn[]).forEach(fn => { propFnResList.value.push(fn()) })
  }
</script>