不是很理解 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 已经被清理了,所以它的依赖发生变化,也不用再通知它更新了。

0
2
ustbhuangyi
当你不需要的时候,你才会调用这个方法
2020-01-10
共2条回复

Vue.js 源码深入解析 深入理解Vue实现原理

全方位讲解 Vue.js 源码,进阶高级工程师

4984 学习 · 1037 问题

查看课程