computed.uvue 896 字节
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6
<template>
  <view class="page">
    <text id="count" class="uni-common-mb">count: {{ count }}</text>
    <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>
7
    <button class="update-btn" @click="update">update</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  </view>
</template>

<script setup>
  const count = ref(0)

  const doubleCount = computed<number>(() : number => {
    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>