前言上一节着重讲述了initComputed中的代码,以及数据是如何从computed中到视图层的,以及data修改后如何作用于computed。这一节主要记录initWatcher中的内容。正文demo修改之前的new Vue(options)的options中,我们可以观察到computed,data,但是对于watch是没法演示的,所以我们在代码中加入一段可以观察到watch初始化以及效果的代码。{ watch: { a(newV,oldV) { console.log(${oldV} -> ${newV}
); } }}依旧是观察a这个变量,当点击+1按钮时候,即可让a变化。入口if (opts.watch && opts.watch !== nativeWatch) { initWatch(vm, opts.watch);}initWatchfunction initWatch (vm: Component, watch: Object) { for (const key in watch) { // 拿到watch中相关的处理逻辑 const handler = watch[key] // 如果是个数组,就挨个创建watcher if (Array.isArray(handler)) { for (let i = 0; i < handler.length; i++) { createWatcher(vm, key, handler[i]) } } else { // 否则直接初始化,我们此次例子中会直接走这里 // createWatcher(vm, a, fn) createWatcher(vm, key, handler) } }}createWatcherfunction createWatcher ( vm: Component, keyOrFn: string | Function, handler: any, options?: Object) { // 这里只是对不同创建形式的标准化。 if (isPlainObject(handler)) { options = handler handler = handler.handler } if (typeof handler === ‘string’) { handler = vm[handler] } // 最终这里是真正监听值变化的地方。 // $watch(a, handle, undefined); return vm.$watch(keyOrFn, handler, options)}vm.$watchVue.prototype.$watch = function ( expOrFn: string | Function, cb: any, options?: Object ): Function { const vm: Component = this if (isPlainObject(cb)) { return createWatcher(vm, expOrFn, cb, options) } options = options || {} options.user = true // 主要是这里创建一个观察者 // new Watcher(vm, ‘a’, handle, {user: true}) const watcher = new Watcher(vm, expOrFn, cb, options) if (options.immediate) { cb.call(vm, watcher.value) } return function unwatchFn () { watcher.teardown() } }new Watcherwatcher,即观察者,是我们多次提到的一个东西。这里主要强调的是watcher.get()中的内容。class Watcher { constructor ( vm: Component, expOrFn: string | Function, cb: Function, options?: Object ) { // 一堆初始化信息 if (options) { this.deep = !!options.deep this.user = !!options.user this.lazy = !!options.lazy this.sync = !!options.sync } else { this.deep = this.user = this.lazy = this.sync = false } this.value = this.lazy ? undefined : this.get() } get () { // 将当前watcher设为Dep.target方便后面访问a的getter时候, // 设定为a的依赖 pushTarget(this) let value const vm = this.vm try { // 求a的值。并把当前watcher加入a的依赖中。 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 }}run watcher到上一步,监听a的watcher已经初始化完毕了,当a因为点击鼠标变化时候,会触发这个watcher的变化。执行watcher的run方法,我们继续来看下run方法里的内容。class Watcher { run () { if (this.active) { // 求a的最新值。 const value = this.get() if ( value !== this.value || // Deep watchers and watchers on Object/Arrays should fire even // when the value is the same, because the value may // have mutated. isObject(value) || this.deep ) { // 当a变的时候,将旧值赋给oldValue。 const oldValue = this.value // this.value赋予最新的值。 this.value = value // 用户定义的watch调用时加入try,catch。 if (this.user) { try { // 执行当时传入的回调,并将新值与旧值一并传入。 this.cb.call(this.vm, value, oldValue) } catch (e) { handleError(e, this.vm, callback for watcher "${this.expression}"
) } } else { this.cb.call(this.vm, value, oldValue) } } } }}总结至此,watch,监听属性的这一部分已经完结,本质上就是对于每个监听的属性,创建一个watcher。当watcher改变时候,会触发开发者定义的回调。通过前两篇文章的学习,这篇应该算是很理解的内容。文章链接vue源码分析系列vue源码分析系列之debug环境搭建vue源码分析系列之入口文件分析vue源码分析系列之响应式数据(一)vue源码分析系列之响应式数据(二)vue源码分析系列之响应式数据(三)