vue 可写的computed

 computed可以接受一个get、set函数的对象,变成可写的computed(创建一个可写的 ref 对象)。

<script setup lang="ts">
import { ref, computed } from "vue"

const count = ref(1)

// const plusOne = computed(() => count.value + 1) 只读的计算属性ref

const plusOne = computed({   // 可写的计算属性 ref
  get:() => count.value + 1,
  set:(val) =>{
    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>

相关推荐

  1. vue computed

    2024-01-12 13:32:01       37 阅读
  2. Vuecomputed大致细节

    2024-01-12 13:32:01       8 阅读
  3. vuecomputed 和 watch 区别

    2024-01-12 13:32:01       35 阅读
  4. VueComputed、Methods和Watch

    2024-01-12 13:32:01       48 阅读
  5. vue——computed和methods区别

    2024-01-12 13:32:01       15 阅读
  6. vue computed缓存在哪里

    2024-01-12 13:32:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-12 13:32:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-12 13:32:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-12 13:32:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-12 13:32:01       20 阅读

热门阅读

  1. 学习笔记18——个人理解为什么快速重传是3次ACK

    2024-01-12 13:32:01       40 阅读
  2. html常用类名

    2024-01-12 13:32:01       38 阅读
  3. 数据结构实验5:二叉树的应用

    2024-01-12 13:32:01       24 阅读
  4. libzmq ZMQ_SERVER and ZMQ_CLIENT was not declared in this scope

    2024-01-12 13:32:01       34 阅读
  5. 如何通过 Python 进行服务器监控和故障排查?

    2024-01-12 13:32:01       38 阅读
  6. mysql 优化工具 EXPLAIN详解

    2024-01-12 13:32:01       37 阅读
  7. 127. 单词接龙

    2024-01-12 13:32:01       36 阅读