修改的时候怎么响应
来源:4-10 检测变化的注意事项

旋涡鸣人_
2019-01-26
一般来说一个data 属性 或是 响应式属性 对应的 dep 收集的 watcher 是个数组。。。
什么情况下 一个属性 会收集成多个 watcher(一个属性被多个watchery依赖)呢。。。
dep 里面不是 this.subs 一般收集不是在 get被调用的时候 会pushTarget 会把当前 watcher 订阅。。。,但是 一个属性(data 针对组件定义)的使用范围 貌似 是针对于某个组件,而一个组件 似乎会对应一个 watcher ,理论上还是不存在多个订阅者。。。
我是遗漏了什么吗
depend () {
if (Dep.target) {
Dep.target.addDep(this)
}
}
addSub (sub: Watcher) {
this.subs.push(sub)
}
export function pushTarget (target: ?Watcher) {
targetStack.push(target)
Dep.target = target
}
get () {
pushTarget(this)
let value
const vm = this.vm
try {
value = this.getter.call(vm, vm)
} catch (e) {
if (this.user) {
handleError(e, vm, getter for watcher "${this.expression}"
)
} else {
throw e
}
} finally {
// “touch” every property so they are all tracked as
// dependencies for deep watching
if (this.deep) {
traverse(value)
}
popTarget()
this.cleanupDeps()
}
return value
}
/**
- Add a dependency to this directive.
*/
addDep (dep: Dep) {
const id = dep.id
if (!this.newDepIds.has(id)) {
this.newDepIds.add(id)
this.newDeps.push(dep)
if (!this.depIds.has(id)) {
dep.addSub(this)
}
}
}
1回答
-
除了 render watcher,还有 user watcher 呀,也就是你自定义的 watcher,watch 某个值的时候,那么这个值把该 watcher 收集为它的依赖了
212019-01-27
相似问题
回答 3