【Vue】----- computed与watch的区别
1.computed
- computed是一种计算属性,用来监听属性的变化;
- computed里面的方法调用的时候不需要加(),并且里面的方法必须要有一个返回值;
- computed里面的方法不是通过事件来去触发的,而是当data中的属性发生了改变的时候会被触发;
- computed最大的特点是当属性没有发生改变的时候,当前方法的值会从缓存中读取。
<div id="app">
<input type="text" v-model.number="a">
<input type="text" v-model.number="b">
<button @click="handleAdd()">计算</button>
<p>结果为:{{sum}}</p> <!-- 执行methods中的handleAdd()方法后返回的结果 -->
<p>computed结果:{{count}}</p> <!-- 执行computed中的count()方法后返回的结果 -->
</div>
new Vue({
el:"#app",
data:{
a:"",
b:"",
sum:""
},
methods:{
handleAdd(){
this.sum = this.a+this.b; //只有点击事件触发时才会改变
}
},
computed:{
count(){
return this.a+this.b; //实时监听,只要data中数据发生改变返回的结果就会改变
}
}
})
2. watch
- watch用来监听每一个属性的变化;
- watch这个对象里面都是函数,函数的名称是data中的属性名称,watch中的函数是不需要调用的;
- 当属性发生改变时就会触发watch中的函数,每一个函数都会接受到2个值,一个值是新值,一个是旧值。可以在watch当中进行新旧值的判断来减少虚拟DOM的渲染;
- 只要属性发生改变就会触发它所对应的函数;
- 如果我们需要对对象进行监听的时候,需要将属性设置为key值,val值为一个对象。对象中有2个参数,一个是handler函数,另一个是deep为true,这样才能实现深度监听。
<div id="app">
<input type="text" v-model.number="a">
<input type="text" v-model.number="b">
<p>结果:{{sum}}</p>
<hr>
<input type="text" v-model="obj.name">
<input type="text" v-model="obj.age">
</div>
new Vue({
el:"#app",
data:{
a:"",
b:"",
sum:"",
obj:{
name:"pinpinkc",
age:18
}
},
watch:{
a(newVal,oldVal){
if(newVal != oldVal){
this.sum = newVal+this.b;
}
console.log("a发生了改变",newVal,oldVal)
},
b(newVal,oldVal){
this.sum = newVal+this.a;
console.log("b发生了改变",newVal,oldVal)
},
obj:{
handler(newVal){
console.log("obj发生了改变",newVal)
},
deep:true
}
}
})
3. computed与watch的区别
- computed在调用的时候不需要加() , watch不需要调用;
- computed如果属性没有发生改变的时候会存缓存中读取值 , watch当属性发生改变的时候会接受到2个值,一个为新值,一个为旧值;
- computed里面的函数必须要有一个return返回结果;
- watch如果需要监听对象的情况下必须设置深度监听;
- computed里面函数的名称可以随意命名,但是watch中函数的名称必须是data中属性的名称。
【Vue】----- computed与watch的区别的更多相关文章
- Vue.js中 computed 和 methods 的区别
官方文档中已经有对其的解释了,在这里把我的理解记录一下Vue中的methods.watch.computed computed 的使用场景 HTML模板中的复杂逻辑表达式,为了防止逻辑过重导致不易维护 ...
- vue计算属性computed和methods的区别
computed和methods的区别 在new Vue的配置参数中的computed和methods都可以处理大量的逻辑代码,但是什么时候用哪个属性,要好好区分一下才能做到正确的运用vue. com ...
- vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)
_ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...
- vue中computed/method/watch的区别
摘要:本文通过官方文档结合源码来分析computed/method/watch的区别. Tips:本文分析的源码版本是v2.6.11,文章中牵涉到vue响应式系统原理部分,如果不是很了解,建议先阅读上 ...
- vue computed 原理
vue computed 主要依靠数据依赖来更新,这里不展示computed源代码,只展示核心思想. computed: { a(){ return this.b ++ } } data:{ b: 1 ...
- vue和react之间的区别
1.Vue和React之间的区别 相同点: Vue和其他框架一样,都有组件开发和虚拟dom 都支持props进行父子组件之间的数据通信 都支持数据驱动视图,不直接操作真实dom 都支持服务器端的 渲染 ...
- Vue computed props pass params
Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ...
- vuex bug & vue computed setter
vuex bug & vue computed setter https://vuejs.org/v2/guide/computed.html#Computed-Setter [Vue war ...
- Vue中computed与method的区别
转载于:https://segmentfault.com/a/1190000014478664?utm_source=tag-newest 1.computed区别于method的两个核心 在官方文档 ...
随机推荐
- ie低版本内核事件兼容问题(事件绑定,绑定事件自动执行,文档模式问题)
问题情况 搜狗等,兼容模式下,以前前端写的点击事件的代码没有, 后来一看是因为兼容模式为9,导致点击事件失效 解决办法,步骤 1,处理绑定事件兼容问题 ie低版本绑定事件只支持attactevent, ...
- 下载Spring4.3.18.RELEASE的官方文档
wget -p --page-requisites --convert-links -P /root/spring https://docs.spring.io/spring/docs/4.3.18. ...
- 蛤?你要用html做游戏?(笔记版)
标签(空格分隔):canvas html game 本书是看<html5 Canvas游戏开发实战>(2013)笔记 博主小白,啥也不懂类型,这只是一个笔记,需要的话可以看原书. 书张这样 ...
- C#中添加log4net(日志文件)
1.先下载引用“log4net” 2.然后再App.config配置 3.添加一个LogHandler类 4.在Assemblyinfo类中添加配置的读取文件 5.运用日志文件 6.显示结果
- pycahrm 安装Vue项目
首先在pycharm > preference > plugins > vue > 点绿色install 创建一个文件夹 然后cmd里cd到这个文件夹 npm install ...
- 【原创】XAF Winfrom 支持4K分辨率
Program.cs 添加以下代码 [STAThread] static void Main() { //支持4K显示 DevExpress.XtraEditors.WindowsFormsSetti ...
- [Swift]LeetCode114. 二叉树展开为链表 | Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [Swift]LeetCode442. 数组中重复的数据 | Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- PHP算法之选择排序
//选择排序 $array = [10,203,30,2,4,43]; //第一次从下标为0的开始下标为0的这个数与后面的n-1个进行比较:找出最小或者最大的放在下标为0的这个位置; //第二次从下标 ...