继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Vue's computed and watch

慕九州4199636
关注TA
已关注
手记 1
粉丝 0
获赞 1

computed is focus on compute.

watch is offen use to watch the data's change.

in fact, use one of them can take the finaly aim,but sometimes we need to attention code's simple or complex. we should base on their name to figure out this factor. 

watch :

var vm = new Vue({
  el: '#demo',
  data: {
    firstName: 'Foo',
    lastName: 'Bar',
    fullName: 'Foo Bar'
  },
  watch: {
    firstName: function (val) {
      this.fullName = val + ' ' + this.lastName
    },
    lastName: function (val) {
      this.fullName = this.firstName + ' ' + val
    }
  }
})

computed :

var vm = new Vue({
  el: '#demo',
  data: {
    firstName: 'Foo',
    lastName: 'Bar'
  },
  computed: {
    fullName: function () {
      return this.firstName + ' ' + this.lastName
    }
  }
})

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP