Watch
使用 watch 是用来监控指定的资料变化时,触发相对应的行为,要监控一般的参数很简单,但是要监控物件里面的属性呢?以下是解决的方式:
export default { data() { return { form: { type: 'document' } }; }, watch: { //当 form 里面的 type 改变时则会触发 'form.type': function () { // Do anything ... this.doSomething(); console.log('change'); } }};
在这个程式码中,我们不能将
'form.type': function () {}
这个函数改成箭头函数'form.type' () => {}
,因为箭头函数会绑定parent
的内容,这样会导致this.doSomething()
这行程式码出现错误。
同步收录于部落格