vuejs2:如何销毁观察者?

标签 vuejs2 watch destroy

我如何销毁这个观察者?从父组件加载异步数据后,在子组件中仅需要一次。

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/

相关文章:

javascript - Vue.js 什么时候不会渲染 <input> ?

ios - 在 ITC 审查期间观看 OS3、UIRequiredDeviceCapabilities 问题

http - 如何很好地销毁 Node.js http.request 连接?

c++ - 如何将C++对象的地址传递给C

vue.js - 当 Vuejs 中的 Prop 值发生变化时,DOM 不会更新

vue.js - 在计算属性中访问 $el

vue.js - 如何在计算属性中使用 prop?

iOS:如何在调试时观察 NSManagedObject 属性

delphi - 即使不在 Delphi IDE 中逐步调试,也可以访问监视项

swift - 如果一个函数返回一个 UnsafeMutablePointer,我们有责任销毁和释放吗?