Vue实现双向绑定的原理以及响应式数据的方法

2020-06-14 06:19:51易采站长站整理

this.fullName = val + ' ' + this.lastName
})

vm.$watch('lastName', function (val) {
this.fullName = this.firstName + ' ' + val
}

(2)利用computed监听,更为简洁,同时computed会将结果进行缓存,当结果没有发生变化时,不会触发相应回调


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