我如何销毁这个观察者?从父组件加载异步数据后,在子组件中仅需要一次。
export default {
...
watch: {
data: function(){
this.sortBy();
},
},
...
}
gregor;)
最佳答案
如果通过调用vm。$ watch函数动态地构造观察程序,它将返回一个函数,该函数可能在以后的某个时间点被调用以禁用(删除)该特定的观察程序。
不要像代码中那样将观察者静态地放置在组件中,而是要执行以下操作:
created() {
var unwatch = this.$watch(....)
// now the watcher is watching and you can disable it
// by calling unwatch() somewhere else;
// you can store the unwatch function to a variable in the data
// or whatever suits you best
}
可以从这里找到更详尽的解释:https://codingexplained.com/coding/front-end/vue-js/adding-removing-watchers-dynamically
关于vuejs2:如何销毁观察者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46987893/