vuejs2:我如何销毁观察者?

2024-03-18

我怎样才能摧毁这个观察者?当我的异步数据从父组件加载时,我的子组件中只需要一次。

export default {
    ...
    watch: {
        data: function(){
            this.sortBy();
        },
    },
    ...
}

格雷戈尔;)


如果通过调用 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 https://codingexplained.com/coding/front-end/vue-js/adding-removing-watchers-dynamically

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vuejs2:我如何销毁观察者? 的相关文章

随机推荐