vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

4 - 可写的计算属性

Open kalu5 opened this issue 2 years ago • 0 comments

// 你的答案
<script setup lang="ts">
import { ref, computed } from "vue"

const count = ref(1)
//const plusOne = computed(() => count.value + 1)
const plusOne = computed({
  get () {
    return count.value + 1
  },
  set (val: number) {
    count.value = val - 1
  }
})


/**
 * Make the `plusOne` writable.
 * So that we can get the result `plusOne` to be 3, and `count` to be 2.
*/

plusOne.value++

</script>

<template>
  <div>
    <p>{{ count }}</p>
    <p>{{ plusOne }}</p>
  </div>
</template>

kalu5 avatar Aug 08 '22 02:08 kalu5