vue3 options模式

applyOptions

外部

内部

1.resolveMergedOptions

2.beforeCreate

3.resolveInjections

js 复制代码
    const provides = instance ? instance.parent == null ? instance.vnode.appContext && instance.vnode.appContext.provides : instance.parent.provides : currentApp._context.provides;

4.methods

js 复制代码
   Object.defineProperty(ctx, key, {
            value: methodHandler.bind(publicThis),
            configurable: true,
            enumerable: true,
            writable: true
          });
        } else {
          ctx[key] = methodHandler.bind(publicThis);

5.dataOptions

js 复制代码
    const data = dataOptions.call(publicThis, publicThis);

6.computedOptions

js 复制代码
 for (const key in computedOptions) {
      const opt = computedOptions[key];
      const get =  opt.get.bind(publicThis, publicThis)
      const set = opt.set.bind(publicThis)
      const c = computed({
        get,
        set
      });
      Object.defineProperty(ctx, key, {
        enumerable: true,
        configurable: true,
        get: () => c.value,
        set: (v) => c.value = v
      });

7.watchOptions

js 复制代码
 for (const key in watchOptions) {
      createWatcher(watchOptions[key], ctx, publicThis, key);
    }
    
  const getter = key.includes(".") ? createPathGetter(publicThis, key) : () => publicThis[key];
    const handler = ctx[raw];
   const handler = isFunction(raw.handler) ? raw.handler.bind(publicThis) : ctx[raw.handler];

 watch(getter, handler);
 watch(getter, raw.bind(publicThis));
 watch(getter, handler, raw);

8.provideOptions

js 复制代码
  let provides = currentInstance.provides;
    const parentProvides = currentInstance.parent && currentInstance.parent.provides;
    if (parentProvides === provides) {
      provides = currentInstance.provides = Object.create(parentProvides);
    }
    provides[key] = value;
    if (currentInstance.type.mpType === "app") {
      currentInstance.appContext.app.provide(key, value);
    }

9.created

js 复制代码
    callHook$1(created, instance, "c");

10.registerLifecycleHook

js 复制代码
  function registerLifecycleHook(register, hook) {
    if (isArray(hook)) {
      hook.forEach((_hook) => register(_hook.bind(publicThis)));
    } else if (hook) {
      register(hook.bind(publicThis));
    }
  }
  registerLifecycleHook(onBeforeMount, beforeMount);
  registerLifecycleHook(onMounted, mounted);
  registerLifecycleHook(onBeforeUpdate, beforeUpdate);
  registerLifecycleHook(onUpdated, updated);
  registerLifecycleHook(onActivated, activated);
  registerLifecycleHook(onDeactivated, deactivated);
  registerLifecycleHook(onErrorCaptured, errorCaptured);
  registerLifecycleHook(onRenderTracked, renderTracked);
  registerLifecycleHook(onRenderTriggered, renderTriggered);
  registerLifecycleHook(onBeforeUnmount, beforeUnmount);
  registerLifecycleHook(onUnmounted, unmounted);
  registerLifecycleHook(onServerPrefetch, serverPrefetch);

11.expose

js 复制代码
 if (isArray(expose)) {
    if (expose.length) {
      const exposed = instance.exposed || (instance.exposed = {});
      expose.forEach((key) => {
        Object.defineProperty(exposed, key, {
          get: () => publicThis[key],
          set: (val) => publicThis[key] = val
        });
      });
    } else if (!instance.exposed) {
      instance.exposed = {};
    }
  }

11.render

js 复制代码
if (render && instance.render === NOOP) {
    instance.render = render;
  }

12.inheritAttrs

js 复制代码
 if (inheritAttrs != null) {
    instance.inheritAttrs = inheritAttrs;
  }

13.components

js 复制代码
 if (components)
    instance.components = components;

14.directives

js 复制代码
  if (directives)
    instance.directives = directives;

15.customApplyOptions

js 复制代码
  const customApplyOptions = instance.appContext.config.globalProperties.$applyOptions;
if (customApplyOptions) {
    customApplyOptions(options, instance, publicThis);
  }
相关推荐
SoaringHeart6 小时前
Flutter封装:原生路由管理极简封装 AppNavigator
前端·flutter
menu6 小时前
AI给我的建议
前端
张可爱6 小时前
20251018-JavaScript八股文整理版(上篇)
前端
小小测试开发6 小时前
Python Arrow库:告别datetime繁琐,优雅处理时间与时区
开发语言·前端·python
自律版Zz6 小时前
手写 Promise.resolve:从使用场景到实现的完整推导
前端·javascript
golang学习记6 小时前
从0死磕全栈之Next.js 自定义 Server 指南:何时使用及如何实现
前端
张可爱6 小时前
从奶茶店悟透 JavaScript:递归、继承、浮点数精度、尾递归全解析(通俗易懂版)
前端
梵得儿SHI6 小时前
Vue 开发环境搭建全指南:从工具准备到项目启动
前端·javascript·vue.js·node.js·pnpm·vue开发环境·nvm版本管理
八月ouc7 小时前
每日小知识点:10.14 webpack 有几种文件指纹
前端·webpack