位置:- 正文

vue中computed的详细讲解(vue computed set get)

编辑:rootadmin
vue中computed的详细讲解 vue中computed的详细讲解1.定义2.用法3.computed的响应式依赖(缓存)4.应用场景1.定义

推荐整理分享vue中computed的详细讲解(vue computed set get),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue compute,vue computed mounted,vue comput,vue computed set get,vue comput,vue computed set get,vue computed mounted,vue comput,内容如对您有帮助,希望把文章链接给更多的朋友!

vue中computed的详细讲解(vue computed set get)

       computed是vue的计算属性,是根据依赖关系进行缓存的计算,只有在它的相关依赖发生改变时才会进行更新

2.用法

       一般情况下,computed默认使用的是getter属性

3.computed的响应式依赖(缓存)

       1. computed的每一个计算属性都会被缓存起来,只要计算属性所依赖的属性发生变化,计算属性就会重新执行,视图也会更新。下面代码中,计算属性fullName,它依赖了firstName和lastName这两个属性,只要它们其中一个属性变化,fullName就会重新执行。        2.computed计算属性会被缓存,在下面代码中使用了两次fullName,但在控制台只输出了一次 “这是fullName”。

<template> <div> <div> 姓:<input type="text" v-model="firstName" /> </div> <div> 名:<input type="text" v-model="lastName" /> </div> <!-- 调用两次fullName --> <div>姓名:{{ fullName }}</div> <div>姓名:{{ fullName }}</div> </div></template><script>export default { data() { return { firstName: "张", lastName: "三", }; }, computed: { fullName() { console.log("这是fullName"); return this.firstName + this.lastName; } }};</script>4.应用场景

       当一个数据受多个数据影响时,可以使用computed        1.本组件计算        2.计算props的值        3.计算vuex的state或者getters值的变化

参考:https://cn.vuejs.org/v2/guide/computed.html            https://segmentfault.com/a/1190000012948175

本文链接地址:https://www.jiuchutong.com/zhishi/298895.html 转载请保留说明!
下一篇链接:https://www.jiuchutong.com/zhishi/298896.html
免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络