vue watch 和computed

164 阅读1分钟
var vm = new Vue({
  el: '#app',
  data: {
    num1: 0,
    num2: 0
  },
 watch:{
 'num1':function(newVal,oldVal){
      consolo.log(newVal,oldVal)
  },
  computed:{
  'sum':function(){
  		return this.num1+this.num2
    }
  }
})
  • watch: 当num1的值发生变化,会触发该function
  • computed:计算属性,可直接当成data里面的属性用,本质上是一个方法,在使用时把名称当成属性使用,特点在function里面用到的任何data数据发生变化,就是触发该function。数据会放在缓存中。一定要return