Skip to content

hook 函数

suyan edited this page Jul 9, 2022 · 1 revision
function wrap (app, Vue, method, ctx: BackendContext) {
  const original = Vue.prototype[method]
  if (original) {
    Vue.prototype[method] = function (...args) {
      const res = original.apply(this, args)
      logEvent(this, method, args[0], args.slice(1))
      return res
    }
  }

  function logEvent (vm, type, eventName, payload) {
    // The string check is important for compat with 1.x where the first
    // argument may be an object instead of a string.
    // this also ensures the event is only logged for direct $emit (source)
    // instead of by $dispatch/$broadcast
    if (typeof eventName === 'string' && !internalRE.test(eventName)) {
      const instance = vm._self || vm
      ctx.hook.emit(HookEvents.COMPONENT_EMIT, app, instance, eventName, payload)
    }
  }
}
Clone this wiki locally