Skip to content

$watch

javascript
Vue.prototype.$watch = function (
  expOrFn: string | (() => any),
  cb: any,
  options?: Record<string, any>
): Function {
  // expOrFn 是字符串,即监视的属性。
  // cb 是回调,即属性变化时的回调。
  const vm: Component = this
  if (isPlainObject(cb)) {
    return createWatcher(vm, expOrFn, cb, options)
  }
  options = options || {}
  // 标识是 user watcher
  options.user = true
  // 通过 Watcher 实现
  const watcher = new Watcher(vm, expOrFn, cb, options)
}