Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue的computed中,计算属性如何与属性建立依赖关系? #2

Open
Diazhao opened this issue Jan 28, 2019 · 2 comments
Open

Comments

@Diazhao
Copy link
Owner

Diazhao commented Jan 28, 2019

No description provided.

@Diazhao
Copy link
Owner Author

Diazhao commented Jan 28, 2019

主要是计算属性的getter中收集依赖的过程

@Diazhao Diazhao changed the title vue的computed中,计算属性如何与属性建立以来关系? vue的computed中,计算属性如何与属性建立依赖关系? Jan 28, 2019
@Diazhao
Copy link
Owner Author

Diazhao commented Jan 28, 2019

  1. 属性数据初始化时,通过Object.defineProperty()重写了getter函数,
if(Dep.target){
  dep.depend()
}
  1. 计算属性初始化的时候,首次获取属性值得时候,会调用
    watcher.evalute(),这个函数里面,会进一步调用
    watcher.get(),这个函数里面主要做了
  pushTaget(this)
  value = this.getter.call(vm,vm)
  popTarget()

通过调用getter,进而调用到computed属性对应的get函数,获取到普通属性的时候,会触发getter函数,执行最上面代码块的dep.depend()
这时候dep指向的正是当前计算属性的this,二者正式建立起关联关系

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant