vue3-10-计算属性以及get跟set

html 复制代码
<template>
    <div class=''>
        <h1>计算属性以及get跟set</h1>
        <p>{{ num }}</p>
        <p>{{ dbNum }}</p>
        <button @click="num++">加</button>
        <p>{{ objRet.num }}</p>
        <p>{{ dbNum2 }}</p>
        <button @click="objRet.num++">加</button>
    </div>
</template>

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

let num = ref(20)

// computed 返回值就是计算属性的值,接收返回值之后,可以直接在上面展示
let dbNum = computed(() => {
    return num.value * 2
})

let obj = {
    num: 10
}
let objRet = reactive(obj)
let dbNum2 = computed(() => {
    return objRet.num * 2
})

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

plusOne.value = 1
console.log(count.value) // 0
</script>

<style lang='scss' scoped>

</style>
相关推荐
小云小白4 天前
高性能 v-html 弹窗实现:Vue3 + Element Plus 最佳实践
vue3·弹窗·v-html
xun-ming5 天前
SpringBoot和Vue3实战阿里百炼大模型极简版
spring boot·ai·vue3·智能体·百炼大模型
哆啦A梦15886 天前
20, Springboot3+vue3实现前台轮播图和详情页的设计
javascript·数据库·spring boot·mybatis·vue3
小盼江6 天前
基于Springboot3+Vue3的协同过滤鲜花商城推荐系统
vue3·springboot3·鲜花商城
哆啦A梦15886 天前
11,Springboot3+vue3个人中心,修改密码
java·前端·javascript·数据库·vue3
哆啦A梦15887 天前
01, 前端vue3框架的快速搭建以及项目工程的讲解
前端·vue3·springboot
萧曵 丶11 天前
Vue3组件通信全方案
前端·javascript·vue.js·typescript·vue3
Json____11 天前
vue3-商城管理系统-前端静态网站
前端·vue3·ts·商城纯静态
吴声子夜歌16 天前
Vue3——网络框架Axios的应用
javascript·vue3·axios
赵庆明老师25 天前
vben开发入门6:tsconfig.json
json·vue3·vben