create-vue-app.ts 1.4 KB
Newer Older
1
import { App, Component, createApp, KeepAlive } from 'vue';
2
import MobVue3Components, {
3 4
  i18n,
  loadingDirective,
5 6
} from '@ibiz-template/mob-vue3-components';
import Vant, { allowMultipleToast } from 'vant';
7
import { piniaInstance } from '@ibiz-template/vue3-util';
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import UserRegister from './user-register';

// 允许同时存在多个 toast
allowMultipleToast();

/**
 * 创建 vue3 实例,避免多实例情况下全局方法未成功挂载
 *
 * @author chitanda
 * @date 2022-12-29 11:12:25
 * @export
 * @param {Component} rootComponent
 * @param {IData} [rootProps]
 * @return {*}  {Promise<App<Element>>}
 */
export function createVueApp(
  rootComponent: Component,
  rootProps?: IData,
): App<Element> {
  const app = createApp(rootComponent, rootProps);
28 29 30

  app.component('KeepAlive', KeepAlive);

31 32 33 34
  // 全局 Vue 异常处理
  app.config.errorHandler = function (err: unknown): void {
    ibiz.util.error.handle(err);
  };
35 36 37 38 39 40 41 42 43 44 45

  // 模态等销毁的时候删除全局的app
  if (rootProps) {
    const oldUnMounted = rootProps.unmounted;
    // eslint-disable-next-line no-param-reassign
    rootProps.unmounted = (): void => {
      oldUnMounted();
      window.vueInstances.delete(app);
    };
  }

46
  app.use(i18n);
47 48
  app.use(Vant).use(piniaInstance).use(MobVue3Components).use(UserRegister);

49 50
  window.vueInstances.set(app, app);

51
  ibiz.plugin.register(app);
52 53
  // loading指令
  app.directive('loading', loadingDirective);
54 55
  return app;
}