Skip to content

4 - 可写的计算属性 #812

@zs-max

Description

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

const count = ref(1)
//computed 接受一个具有 get 和 set 函数的对象,用来创建可写的 ref 对象。
const plusOne = computed({
  get: ()=>{
  return count.value + 1 
  },
  set: val=>{ 
   count.value ++
  }
})

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

plusOne.value=1
console.log('count=>'+count.value+'   plusone=>'+plusOne.value)
</script>

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions