手记

Vue's computed and watch

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
    }
  }
})

0人推荐
随时随地看视频
慕课网APP