admin管理员组文章数量:1355670
I would like to disable watcher, so it doesn't trigger certain watcher when created or mounted finish and enable it after these done. What is the proper way to achieve this?
I would like to disable watcher, so it doesn't trigger certain watcher when created or mounted finish and enable it after these done. What is the proper way to achieve this?
Share Improve this question asked Dec 31, 2017 at 11:34 tomJOtomJO 3713 silver badges12 bronze badges1 Answer
Reset to default 7Without seeing what you are doing, it's quite difficult to know exactly what you are trying to achieve. Generally, you should not be handling watchers
yourself, so it's worth taking a look at your code to see if there is a better solution. With that said, it is possible to apply a watcher
at runtime, you can do it inside the mounted
hook, after you have initialised
everything else:
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: ''
}
})
Here's the JSFiddle: https://jsfiddle/fo0vmxf6/
本文标签:
版权声明:本文标题:javascript - How can I disable vue.js watcher while createdmounted methods are yet to be finished? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744051600a2582524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论