计算属性,方法,侦听器

时间:2020-02-19 18:02:46   收藏:0   阅读:60
计算属性某例:
data:{
 a:xx,
 b:yy,
},
computed:{
 c:function() {
  return this.a+this.b
 }
}
调用时插值表达式{{c}}
缓存机制:当计算属性里的变量a,b没有被改变,不会重新计算
 
方法也能实现同样的功能
在methods中定义,但没有缓存机制,所以不如计算属性
调用时插值表达式{{c()}}
 
侦听器:
data:{
 a:xx,
 b:yy,
 c:xxyy //需要初值
},
watch:{
 a:function() {
  this.c=this.a+this.b;
 }, //监听a的变化,同理
 b:function() {
  this.c=this.a+this.b;
 }
}
调用时插值表达式{{c}}
存在缓存机制,但实现过程比较复杂
 
计算属性的setter和getter
computed:{
 c:{ //变成对象的写法
  get:function() {
   return this.a+this.b;
  }, //计算c
  set:function(value) {
   var arr = value.split(‘‘ ‘‘);
   this.a=arr[0];
   this.b=arr[1];
  } //c依赖的值发生变化,c会重新计算,页面刷新
 }
}

原文:https://www.cnblogs.com/lora404/p/12332287.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!