vue中computed/method/watch的区别

摘要:本文通过官方文档结合源码来分析computed/method/watch的区别。

Tips:本文分析的源码版本是v2.6.11,文章中牵涉到vue响应式系统原理部分,如果不是很了解,建议先阅读上一篇文章《深入解析vue响应式原理》。

computed

首先来看官网的解释:计算属性是基于响应式依赖进行缓存的,只在相关响应式依赖发生改变时它们才会重新求值。

下面通过源码来分析computed是怎么实现响应式缓存的:

initComputed

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)
      }
    }
  }
}
  1. 首先创建一个computedWatchers挂到vm上;
  2. 遍历computed属性,依次将单个computed属性的get方法作为参数创建Watcher实例保存到computedWatchers中;
  3. 再将单个computed属性作为参数传入defineComputed方法。

defineComputed

export function defineComputed (
  target: any,
  key: string,
  userDef: Object | Function
) {
  const shouldCache = !isServerRendering()
  if (typeof userDef === 'function') {
    sharedPropertyDefinition.get = shouldCache
      ? createComputedGetter(key)
      : createGetterInvoker(userDef)
    sharedPropertyDefinition.set = noop
  } else {
    sharedPropertyDefinition.get = userDef.get
      ? shouldCache && userDef.cache !== false
        ? createComputedGetter(key)
        : createGetterInvoker(userDef.get)
      : noop
    sharedPropertyDefinition.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)
}

function createComputedGetter (key) {
  return function computedGetter () {
    const watcher = this._computedWatchers && this._computedWatchers[key]
    if (watcher) {
      if (watcher.dirty) {
        watcher.evaluate()
      }
      if (Dep.target) {
        watcher.depend()
      }
      return watcher.value
    }
  }
}

function createGetterInvoker(fn) {
  return function computedGetter () {
    return fn.call(this, this)
  }
}

将计算属性挂到vm上,定义getter属性方法,方法会执行计算属性获取新值(新值会存储到Watcher.value中)及收集依赖该computed属性的视图。

总结:

  1. 页面初始渲染时,读取computed属性值,computed属性值的getter函数读取data数据,触发data的getter方法,将computed属性对应的Watcher绑定到data的依赖收集器Dep中;
  2. computed属性getter方法中,还会调用Watcher.depend方法,将上层视图的观察者也添加到data的依赖收集器Dep中;
  3. data属性值变更后,将会调用Dep.notify方法,通知所有依赖的Watcher进行update方法;
  4. 首先触发computed关联Watcher的update方法,由于lazy为true,将会设置dirty为true,表示computed属性依赖的data值已经变更,但不会调用Watcher的get方法获取新值。
  5. 然后触发视图关联Watcher的update方法,在更新页面时会调用computed属性值,触发定义的getter函数。由于当前dirty为true,会执行关联Watcher.get方法获取新值,更新Watcher.value的值,并返回新值,完成页面的重新渲染。

method

function initMethods (vm: Component, methods: Object) {
  const props = vm.$options.props
  for (const key in methods) {
    if (process.env.NODE_ENV !== 'production') {
      if (typeof methods[key] !== 'function') {
        warn(
          `Method "${key}" has type "${typeof methods[key]}" in the component definition. ` +
          `Did you reference the function correctly?`,
          vm
        )
      }
      if (props && hasOwn(props, key)) {
        warn(
          `Method "${key}" has already been defined as a prop.`,
          vm
        )
      }
      if ((key in vm) && isReserved(key)) {
        warn(
          `Method "${key}" conflicts with an existing Vue instance method. ` +
          `Avoid defining component methods that start with _ or $.`
        )
      }
    }
    vm[key] = typeof methods[key] !== 'function' ? noop : bind(methods[key], vm)
  }
}

method的源码很简单,只是绑定了所有method的this为vm。

watch

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)
    }
  }
}

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)
}

 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) {
      try {
        cb.call(vm, watcher.value)
      } catch (error) {
        handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
      }
    }
    return function unwatchFn () {
      watcher.teardown()
    }
  }

Tips:Watcher对象源码在上文中《深入解析vue响应式原理》分析过,这里就不再赘述。

经过分析watch的源码可以发现,实际上每一个watch属性对应生成了一个Watcher对象,通过获取data属性值将Watcher添加到依赖收集器Dep中,当data数据更新时,就会调用Dep.notify通知Watcher。

总结:

  1. computed属性是根据依赖的data属性(有可能多个)进行更新标记,等视图获取该computed属性数据时才执行更新计算,并将值缓存到对应的Watcher实例中。且只有当data数据变化后,视图读取关联computed属性才会重新计算结果。
  2. method属性是根据视图需要即时计算获得,不具有缓存性质,当关联data数据没有更新时也会重新计算。
  3. watch属性是有且只能依赖单个data属性,当data数据变化后,会立即触发Watcher.update,调用对应watch定义的方法执行。不具有缓存性质。