1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { App, Component, createApp, KeepAlive } from 'vue';
import MobVue3Components, {
i18n,
loadingDirective,
} from '@ibiz-template/mob-vue3-components';
import Vant, { allowMultipleToast } from 'vant';
import { piniaInstance } from '@ibiz-template/vue3-util';
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);
app.component('KeepAlive', KeepAlive);
// 全局 Vue 异常处理
app.config.errorHandler = function (err: unknown): void {
ibiz.util.error.handle(err);
};
// 模态等销毁的时候删除全局的app
if (rootProps) {
const oldUnMounted = rootProps.unmounted;
// eslint-disable-next-line no-param-reassign
rootProps.unmounted = (): void => {
oldUnMounted();
window.vueInstances.delete(app);
};
}
app.use(i18n);
app.use(Vant).use(piniaInstance).use(MobVue3Components).use(UserRegister);
window.vueInstances.set(app, app);
ibiz.plugin.register(app);
// loading指令
app.directive('loading', loadingDirective);
return app;
}