在Vue实例初始化的过程中,initState
方法会调用initComputed
和initWatch
来分别初始化计算属性和侦听属性,那么接下来就分析这两个方法的实现。
计算属性
这两个方法都定义在core/instance/state.js
中
function initComputed (vm: Component, computed: Object) {
// $flow-disable-line
const watchers = vm._computedWatchers = Object.create(null)
// computed properties are just getters during SSR
const isSSR = isServerRendering()
for (const key in computed) {
const userDef = computed[key]
const getter = typeof userDef === 'function' ? userDef : userDef.get
if (process.env.NODE_ENV !== 'production' && getter == null) {
warn(
`Getter is missing for computed property "${key}".`,
vm
)
}
if (!isSSR) {
// create internal watcher for the computed property.
watchers[key] = new Watcher(
vm,
getter || noop,
noop,
computedWatcherOptions
)
}
// component-defined computed properties are already defined on the
// component prototype. We only need to define computed properties defined
// at instantiation here.
if (!(key in vm)) {
defineComputed(vm, key, userDef)
} else if (process.env.NODE_ENV !== 'production') {
if (key in vm.$data) {
warn(`The computed property "${key}" is already defined in data.`, vm)
} else if (vm.$options.props && key in vm.$options.props) {
warn(`The computed property "${key}" is already defined as a prop.`, vm)
}
}
}
}
核心是首先是创建了vm._computedWatchers
属性并设为一个空对象,然后对每个定义的计算属性调用创建一个Watcher并调用defineComputed
方法:
export function defineComputed (
target: any,
key: string,
userDef: Object | Function
) {
const shouldCache = !isServerRendering()
if (typeof userDef === 'function') {
sharedPropertyDefinition.get = shouldCache
? createComputedGetter(key)
: userDef
sharedPropertyDefinition.set = noop
} else {
sharedPropertyDefinition.get = userDef.get
? shouldCache && userDef.cache !== false
? createComputedGetter(key)
: userDef.get
: noop
sharedPropertyDefinition.set = userDef.set
? userDef.set
: noop
}
if (process.env.NODE_ENV !== 'production' &&
sharedPropertyDefinition.set === noop) {
sharedPropertyDefinition.set = function () {
warn(
`Computed property "${key}" was assigned to but it has no setter.`,
this
)
}
}
Object.defineProperty(target, key, sharedPropertyDefinition)
}
计算属性的定义可以是一个函数或者一个对象。如果是一个函数,那么它将作为这个计算属性的getter。如果是一个对象,那么那么这个对象的get
属性和set
属性分别是这个计算属性的getter和setter,同时可以设置cache=false
来禁止缓存。
若果是需要缓存的情况,getter将被设为createComputedGetter
方法的返回值。
function createComputedGetter (key) {
return function computedGetter () {
const watcher = this._computedWatchers && this._computedWatchers[key]
if (watcher) {
watcher.depend()
return watcher.evaluate()
}
}
}
计算属性创建的Watcher与普通的Watcher的不同之处是,在Watcher的构造函数中有这么一段逻辑:
constructor (
vm: Component,
expOrFn: string | Function,
cb: Function,
options?: ?Object,
isRenderWatcher?: boolean
) {
// ...
if (this.computed) {
this.value = undefined
this.dep = new Dep()
} else {
this.value = this.get()
}
}
在组件挂载时创建的Watcher,this.value
是updateComponent
函数,也就是创建Watcher的时候会立即做一次组件更新。而计算属性创建的Watcher没有立即调用this.value
,而是创建了一个Dep
实例。
当组件渲染时访问到计算属性,就会调用它的getter。首先会拿到它对应的watcher,执行watcher.depend()
:
depend () {
if (this.dep && Dep.target) {
this.dep.depend()
}
}
此时Dep.target
是当前正在渲染的组件的Watcher,也就是让当前的活动正在渲染的组件订阅了这个计算属性的变化。
然后调用了watcher.evaluate
:
evaluate () {
if (this.dirty) {
this.value = this.get()
this.dirty = false
}
return this.value
}
如果this.dirty
为true
那么就执行this.get()
,返回计算属性的值。
this.get()
会调用pushTarget(this)
将Dep.target
设为自身,那么如果在执行getter的过程中依赖了其他的响应式属性,就会触发它们的getter,这样就会把它们的dep添加到当前watcher中,使当前计算属性的watcher订阅以来的响应式属性的变化。
一旦计算属性依赖的数据被修改,就会触发 setter,执行watcher.update()
方法通知watcher更新。
update () {
/* istanbul ignore else */
if (this.computed) {
// A computed property watcher has two modes: lazy and activated.
// It initializes as lazy by default, and only becomes activated when
// it is depended on by at least one subscriber, which is typically
// another computed property or a component's render function.
if (this.dep.subs.length === 0) {
// In lazy mode, we don't want to perform computations until necessary,
// so we simply mark the watcher as dirty. The actual computation is
// performed just-in-time in this.evaluate() when the computed property
// is accessed.
this.dirty = true
} else {
// In activated mode, we want to proactively perform the computation
// but only notify our subscribers when the value has indeed changed.
this.getAndInvoke(() => {
this.dep.notify()
})
}
} else if (this.sync) {
this.run()
} else {
queueWatcher(this)
}
}
如果当前计算属性没有被任何组件依赖,就仅仅把this.dirty
设置为true
。否则重新计算属性值,然后通知组件重新渲染。
侦听属性
侦听属性的初始化是在initWatch
方法中:
function initWatch (vm: Component, watch: Object) {
for (const key in watch) {
const handler = watch[key]
if (Array.isArray(handler)) {
for (let i = 0; i < handler.length; i++) {
createWatcher(vm, key, handler[i])
}
} else {
createWatcher(vm, key, handler)
}
}
}
···
`initWatch`方法对每个侦听属性的回调函数执行`createWatcher(vm, key, handler)`。如果一个侦听属性有多个回调函数可以使用一个数组。handler还可以是一个包含options的Object
function createWatcher (
vm: Component,
expOrFn: string | Function,
handler: any,
options?: Object
) {
if (isPlainObject(handler)) {
options = handler
handler = handler.handler
}
if (typeof handler === 'string') {
handler = vm[handler]
}
return vm.$watch(expOrFn, handler, options)
}
`createWatcehr`最终会调用`vm.$watch`方法。`vm.$watch`是Vue原型上的方法,它是在执行`stateMixin`时添加到原型上的。
export function stateMixin (Vue: Class<Component>) {
// ...
Vue.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
const watcher = new Watcher(vm, expOrFn, cb, options)
if (options.immediate) {
cb.call(vm, watcher.value)
}
return function unwatchFn () {
watcher.teardown()
}
}
}
通过`vm.$watch`方法创建的watcher是一个user watcher。用来观察Vue实例上的一个响应式属性的变化,在变化时执行回调函数。它可以有`immediate`或`deep`的选项。
user watcher的并没有很大的区别,只有逻辑上的不同,这里就不分析了。
网友评论