computed.uvue 884 字节
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2
<template>
  <view class="page">
3
    <text id="count" class="uni-common-mb">count: {{ count }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
4 5 6
    <text id="double-count" class="uni-common-mb">computed double count: {{ doubleCount }}</text>
    <text id="obj-arr" class="uni-common-mb">obj.arr: {{ obj.arr }}</text>
    <text id="obj-arr-len" class="uni-common-mb">computed obj.arr.length: {{ objArrLen }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
7
    <button id="update-btn" @click="update">update</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9 10 11 12 13
  </view>
</template>

<script setup>
  const count = ref(0)

14
  const doubleCount = computed(() : number => {
DCloud-WZF's avatar
DCloud-WZF 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    return count.value * 2
  })

  type Obj = {
    arr : number[]
  }

  const obj = reactive({
    arr: [1, 2, 3]
  } as Obj)


  const objArrLen = computed<number>(() : number => {
    return obj.arr.length
  })

  const update = () => {
    count.value++
    obj.arr.push(obj.arr.length + 1)
  }
</script>