不是很理解 teardown 的作用
来源:5-3 计算属性 VS 侦听属性(3)

六一888
2020-01-10
teardown () {
if (this.active) {
// remove self from vm's watcher list
// this is a somewhat expensive operation so we skip it
// if the vm is being destroyed.
if (!this.vm._isBeingDestroyed) {
remove(this.vm._watchers, this)
}
let i = this.deps.length
while (i--) {
this.deps[i].removeSub(this)
}
this.active = false
}
}
$watch 将这个方法返回,可以详细说下这个 teardown 是什么作用么?
写回答
1回答
-
ustbhuangyi
2020-01-10
teardown 就是做清理操作,首先把自身从 vm._watcher 中移除,然后遍历它收集的依赖,删除对它的订阅,因为这个 watcher 已经被清理了,所以它的依赖发生变化,也不用再通知它更新了。
022020-01-10