我想禁用观察者,所以它不会在创建或安装完成时触发某些观察者,并在完成后启用它。实现这一目标的正确方法是什么?
如果没有看到你在做什么,就很难确切地知道你想要实现什么。通常,您不应该watchers自己处理,因此值得查看您的代码以查看是否有更好的解决方案。话虽如此,可以watcher在运行时应用 a ,您可以mounted在拥有initialised其他所有内容后在钩子内执行此操作:
new Vue({
el: '#app',
created() {
// this won't fire the watcher as it hasn't been applied yet
this.foo = 'foo'
},
mounted() {
// Apply watcher after all other initialization is done
this.applyFooWatcher()
},
methods: {
applyFooWatcher() {
this.$watch('foo', function(newVal, oldVal) {
console.log('Foo changed from ' + oldVal + ' to ' + newVal);
});
}
},
data: {
foo: ''
}
})
Run Code Online (Sandbox Code Playgroud)
这是 JSFiddle:https ://jsfiddle.net/fo0vmxf6/
| 归档时间: |
|
| 查看次数: |
2113 次 |
| 最近记录: |