We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
computed传入的其实还是一个函数;
Watcher的本质,其实就是存储了一个需要在特定时机触发的函数,在Vue内部,每个computed属性也有自己的一个对应的watcher实例,下文中叫它computedWatcher computedWatcher的特点:
// 渲染函数watcher new Watcher(() => { document.getElementById('app').innerHTML = ` computed: 1 + number 是 ${computedNumber.value} ` })
因此,我们实现的过程是这样的
当data.number更新时,触发computedWatcher.update;computedWatcher的dep里装着渲染watcher,所以只需要触发 this.dep.notify(),就会触发渲染watcher的update方法,从而更新视图。
更新的路径是这样的 data.number = 5 -> computedWatcher -> 渲染watcher -> 更新视图
//computed.js import Watcher from "./watcher" export default function computed( getter ){ let def = {}; const computedWatcher = new Watcher( getter, {computed: true}) Object.defineProperty(def, "value", { get(){ computedWatcher.dep.depend(); return computedWatcher.get(); } }) return def; }
import Dep, {pushTarget, popTarget } from "./dep" export default class Watcher { constructor( getter, options = {} ){ let {computed, watch, callback} = options; this.getter = getter; this.computed = computed; this.watch = watch; this.callback = callback; this.value = undefined; if(computed){ this.dep = new Dep(); }else{ this.get(); } } depend(){ this.dep.depend() } get(){ pushTarget(this); this.value = this.getter(); popTarget(); return this.value } update(){ if(this.computed){ this.get(); this.dep.notify(); }else{ this.get(); } } }
参考:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
vue计算属性 computed
computed传入的其实还是一个函数;
实现本质:computed在读取属性值时,此时的Dep.target是正在运行的渲染函数的watcher
因此,我们实现的过程是这样的
参考:
The text was updated successfully, but these errors were encountered: