@ctzy-web-client/web-base-client-vue 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/_virtual/_plugin-vue_export-helper.mjs +9 -0
- package/es/_virtual/_rollupPluginBabelHelpers.mjs +2830 -0
- package/es/application/Application2.mjs +42 -0
- package/es/application/MicroApplication.mjs +49 -0
- package/es/application/app.mjs +34 -0
- package/es/application/application.mjs +312 -0
- package/es/application/index.mjs +11 -0
- package/es/application/micro-application.mjs +59 -0
- package/es/constants/index.mjs +1 -0
- package/es/hooks/index.mjs +11 -0
- package/es/hooks/use-data-form/index.mjs +18 -0
- package/es/hooks/use-data-table/index.mjs +18 -0
- package/es/hooks/use-data-table-page/index.mjs +47 -0
- package/es/hooks/use-event-dispatcher/index.mjs +14 -0
- package/es/hooks/use-global-config/index.mjs +47 -0
- package/es/hooks/use-keydown/index.mjs +2 -0
- package/es/hooks/use-keydown/use-keydown.mjs +24 -0
- package/es/hooks/use-keydown/use-save-keydown.mjs +12 -0
- package/es/hooks/use-namespace/index.mjs +68 -0
- package/es/hooks/use-service/index.mjs +9 -0
- package/es/hooks/use-value/index.mjs +9 -0
- package/es/index.mjs +20 -0
- package/es/interceptors/RequestInterceptors.mjs +25 -0
- package/es/interceptors/RouterInterceptors.mjs +36 -0
- package/es/interceptors/index.mjs +2 -0
- package/es/ioc/ReactiveInstantiationService.mjs +14 -0
- package/es/ioc/index.mjs +1 -0
- package/es/utils/index.mjs +1 -0
- package/es/utils/pageDataParser.mjs +21 -0
- package/lib/_virtual/_plugin-vue_export-helper.js +13 -0
- package/lib/_virtual/_rollupPluginBabelHelpers.js +2948 -0
- package/lib/application/Application2.js +46 -0
- package/lib/application/MicroApplication.js +53 -0
- package/lib/application/app.js +38 -0
- package/lib/application/application.js +316 -0
- package/lib/application/index.js +18 -0
- package/lib/application/micro-application.js +63 -0
- package/lib/constants/index.js +2 -0
- package/lib/hooks/index.js +30 -0
- package/lib/hooks/use-data-form/index.js +22 -0
- package/lib/hooks/use-data-table/index.js +22 -0
- package/lib/hooks/use-data-table-page/index.js +51 -0
- package/lib/hooks/use-event-dispatcher/index.js +18 -0
- package/lib/hooks/use-global-config/index.js +52 -0
- package/lib/hooks/use-keydown/index.js +11 -0
- package/lib/hooks/use-keydown/use-keydown.js +28 -0
- package/lib/hooks/use-keydown/use-save-keydown.js +16 -0
- package/lib/hooks/use-namespace/index.js +73 -0
- package/lib/hooks/use-service/index.js +13 -0
- package/lib/hooks/use-value/index.js +13 -0
- package/lib/index.js +68 -0
- package/lib/interceptors/RequestInterceptors.js +29 -0
- package/lib/interceptors/RouterInterceptors.js +40 -0
- package/lib/interceptors/index.js +11 -0
- package/lib/ioc/ReactiveInstantiationService.js +18 -0
- package/lib/ioc/index.js +9 -0
- package/lib/utils/index.js +9 -0
- package/lib/utils/pageDataParser.js +25 -0
- package/package.json +37 -0
- package/src/application/Application.js +538 -0
- package/src/application/MicroApplication.js +67 -0
- package/src/application/app.vue +22 -0
- package/src/application/application.vue +36 -0
- package/src/application/index.js +11 -0
- package/src/application/micro-application.vue +56 -0
- package/src/constants/index.js +0 -0
- package/src/hooks/index.js +9 -0
- package/src/hooks/use-data-form/index.js +24 -0
- package/src/hooks/use-data-table/index.js +22 -0
- package/src/hooks/use-data-table-page/index.js +48 -0
- package/src/hooks/use-event-dispatcher/index.js +15 -0
- package/src/hooks/use-global-config/index.js +49 -0
- package/src/hooks/use-keydown/index.js +2 -0
- package/src/hooks/use-keydown/use-keydown.js +35 -0
- package/src/hooks/use-keydown/use-save-keydown.js +13 -0
- package/src/hooks/use-namespace/index.js +90 -0
- package/src/hooks/use-service/index.js +15 -0
- package/src/hooks/use-value/index.js +15 -0
- package/src/index.js +8 -0
- package/src/interceptors/RequestInterceptors.js +46 -0
- package/src/interceptors/RouterInterceptors.js +47 -0
- package/src/interceptors/index.js +2 -0
- package/src/ioc/ReactiveInstantiationService.js +29 -0
- package/src/ioc/index.js +1 -0
- package/src/utils/index.js +1 -0
- package/src/utils/pageDataParser.js +27 -0
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { createApp, reactive, h } from 'vue';
|
|
2
|
+
import {
|
|
3
|
+
createRouter,
|
|
4
|
+
createWebHashHistory,
|
|
5
|
+
createWebHistory,
|
|
6
|
+
createMemoryHistory,
|
|
7
|
+
} from 'vue-router';
|
|
8
|
+
import { EventDispatcher, HttpRequest } from '@ctzy-web-client/support';
|
|
9
|
+
import {
|
|
10
|
+
IocContainer,
|
|
11
|
+
InstantiationService,
|
|
12
|
+
SERVICE_INFO,
|
|
13
|
+
CLASS_ID,
|
|
14
|
+
} from '@ctzy-web-client/ioc';
|
|
15
|
+
import { DataModelAdapter, SimpleOrm } from '@ctzy-web-client/data-model';
|
|
16
|
+
import { MicroApplication } from './MicroApplication';
|
|
17
|
+
import { pageDataParser } from '../utils';
|
|
18
|
+
import App from './app.vue';
|
|
19
|
+
import { RequestInterceptors, RouterInterceptors } from '../interceptors';
|
|
20
|
+
import { ReactiveInstantiationService } from '../ioc';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* web 应用配置
|
|
24
|
+
* @typedef {Object} WebApplictionOption
|
|
25
|
+
* @property {Object} request 请求相关的配置
|
|
26
|
+
* @property {String} request.baseURL 请求基础路径
|
|
27
|
+
* @property {String|Number} request.timeout 请求超时时间
|
|
28
|
+
* @property {String} routeBaseUrl 路由基础路径 默认 /
|
|
29
|
+
* @property {String} routeMode 路由模式 hash | history
|
|
30
|
+
* @property {Array<Object>|Object} pages 注册的页面
|
|
31
|
+
* @property {Array<Object>|Object} pages[].parent 父路由
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export class Application extends EventDispatcher {
|
|
35
|
+
constructor(appName, options) {
|
|
36
|
+
super();
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @type {string}
|
|
40
|
+
*/
|
|
41
|
+
this.appName = appName;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @type {WebApplictionOption}
|
|
45
|
+
*/
|
|
46
|
+
this.options = options || {};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @private
|
|
50
|
+
* @type {import('vue').App}
|
|
51
|
+
*/
|
|
52
|
+
this._app = null;
|
|
53
|
+
/**
|
|
54
|
+
* @private
|
|
55
|
+
* @type {import('vue-router').Router}
|
|
56
|
+
*/
|
|
57
|
+
this._router = null;
|
|
58
|
+
|
|
59
|
+
this._history = null;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @private
|
|
63
|
+
* @type {IocContainer}
|
|
64
|
+
*/
|
|
65
|
+
this._iocContainer = new IocContainer();
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @type {Application}
|
|
69
|
+
*/
|
|
70
|
+
this.parent = null;
|
|
71
|
+
/**
|
|
72
|
+
* @type {MicroApplication[]}
|
|
73
|
+
*/
|
|
74
|
+
this.microApplications = [];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @type {Application[]}
|
|
78
|
+
*/
|
|
79
|
+
this.subApplications = [];
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @private
|
|
83
|
+
* @type {string}
|
|
84
|
+
*/
|
|
85
|
+
this._targetSelector = '';
|
|
86
|
+
|
|
87
|
+
this._extendOptions = null;
|
|
88
|
+
|
|
89
|
+
this._plugins = [];
|
|
90
|
+
|
|
91
|
+
this.global = options.global;
|
|
92
|
+
|
|
93
|
+
this.applicationSlots = new Map();
|
|
94
|
+
|
|
95
|
+
if (!this.global) {
|
|
96
|
+
throw new Error('options.global 不存在');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @private
|
|
101
|
+
* @type {Promise<void>}
|
|
102
|
+
*/
|
|
103
|
+
this._viewportMountPromise = null;
|
|
104
|
+
|
|
105
|
+
this.global.__INNER_APPLICATION__ = this;
|
|
106
|
+
|
|
107
|
+
this._initEvent();
|
|
108
|
+
|
|
109
|
+
this._initIocContainer();
|
|
110
|
+
this._setRequestInterceptors();
|
|
111
|
+
|
|
112
|
+
this._exportLifecycle();
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @type {Promise<void>}
|
|
116
|
+
*/
|
|
117
|
+
this._resolveRoutesPromise = null;
|
|
118
|
+
|
|
119
|
+
this._mountResolve = null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
addApplicationSlot(type, id, slot) {
|
|
123
|
+
let withTypeSlots = this.applicationSlots.get(type);
|
|
124
|
+
|
|
125
|
+
if (!withTypeSlots) {
|
|
126
|
+
withTypeSlots = new Map();
|
|
127
|
+
this.applicationSlots.set(type, withTypeSlots);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
withTypeSlots.set(id, slot);
|
|
131
|
+
|
|
132
|
+
this.emit('slot-change', type);
|
|
133
|
+
this.emitParentApplication('slot-change', type);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
removeApplicationSlot(type, id) {
|
|
137
|
+
const withTypeSlots = this.applicationSlots.get(type);
|
|
138
|
+
|
|
139
|
+
if (!withTypeSlots) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
withTypeSlots.delete(id);
|
|
144
|
+
|
|
145
|
+
this.emit('slot-change', type);
|
|
146
|
+
this.emitParentApplication('slot-change', type);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getApplicationSlots(type) {
|
|
150
|
+
let withTypeSlots = this.applicationSlots.get(type);
|
|
151
|
+
if (!withTypeSlots) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
return Array.from(withTypeSlots.values());
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getIocContainer() {
|
|
158
|
+
return this._iocContainer;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
getInstantiationService() {
|
|
162
|
+
return this.getIocContainer().getService(InstantiationService);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
createInstance(Clazz, options) {
|
|
166
|
+
return this.getInstantiationService().createInstance(Clazz, options);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @private
|
|
171
|
+
* @memberof Application
|
|
172
|
+
*/
|
|
173
|
+
_initIocContainer() {
|
|
174
|
+
this._iocContainer.setInstantiationService(ReactiveInstantiationService);
|
|
175
|
+
this._iocContainer.registerService(
|
|
176
|
+
RequestInterceptors,
|
|
177
|
+
RequestInterceptors,
|
|
178
|
+
{ overwrite: true }
|
|
179
|
+
);
|
|
180
|
+
this._iocContainer.registerService(RouterInterceptors, RouterInterceptors, {
|
|
181
|
+
overwrite: true,
|
|
182
|
+
});
|
|
183
|
+
this._iocContainer.registerService(HttpRequest, HttpRequest, {
|
|
184
|
+
overwrite: true,
|
|
185
|
+
args: [this.options.request || {}],
|
|
186
|
+
});
|
|
187
|
+
this._iocContainer.registerService(SimpleOrm, SimpleOrm, {
|
|
188
|
+
overwrite: false,
|
|
189
|
+
});
|
|
190
|
+
this._iocContainer.registerService(DataModelAdapter, DataModelAdapter, {
|
|
191
|
+
overwrite: false,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
this._iocContainer.registerService('Application', Application, {
|
|
195
|
+
overwrite: false,
|
|
196
|
+
});
|
|
197
|
+
this._iocContainer._serviceCollection.setInstance('Application', this);
|
|
198
|
+
|
|
199
|
+
// this._iocContainer.registerService(
|
|
200
|
+
// InstantiationService,
|
|
201
|
+
// ReactiveInstantiationService,
|
|
202
|
+
// {
|
|
203
|
+
// overwrite: false,
|
|
204
|
+
// }
|
|
205
|
+
// );
|
|
206
|
+
this._iocContainer.registerValue('AppName', this.appName || '');
|
|
207
|
+
|
|
208
|
+
const delayRegisterServices = this.global.__DELAY_REGISTER_SERVICES__ || [];
|
|
209
|
+
|
|
210
|
+
for (const Service of delayRegisterServices) {
|
|
211
|
+
const serviceOptions = Service[SERVICE_INFO];
|
|
212
|
+
this._iocContainer.registerService(
|
|
213
|
+
serviceOptions.value,
|
|
214
|
+
Service,
|
|
215
|
+
serviceOptions
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
Reflect.deleteProperty(this.global, '__DELAY_REGISTER_SERVICES__');
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
_setRequestInterceptors() {
|
|
223
|
+
/**
|
|
224
|
+
* @type {RequestClient}
|
|
225
|
+
*/
|
|
226
|
+
const httpRequest = this._iocContainer.getService(HttpRequest);
|
|
227
|
+
|
|
228
|
+
const requestInterceptors =
|
|
229
|
+
this._iocContainer.getService(RequestInterceptors);
|
|
230
|
+
//设置请求相关的配置
|
|
231
|
+
httpRequest.setOptions(this.options.request);
|
|
232
|
+
httpRequest.addRequestInterceptors(
|
|
233
|
+
requestInterceptors.request.bind(requestInterceptors),
|
|
234
|
+
requestInterceptors.requestError.bind(requestInterceptors)
|
|
235
|
+
);
|
|
236
|
+
httpRequest.addResponseInterceptors(
|
|
237
|
+
requestInterceptors.response.bind(requestInterceptors),
|
|
238
|
+
requestInterceptors.requestError.bind(requestInterceptors)
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
_setRouteInterceptors() {
|
|
243
|
+
const routerInterceptors =
|
|
244
|
+
this._iocContainer.getService(RouterInterceptors);
|
|
245
|
+
|
|
246
|
+
this._router.beforeEach(
|
|
247
|
+
routerInterceptors.beforeEach.bind(routerInterceptors)
|
|
248
|
+
);
|
|
249
|
+
this._router.beforeResolve(
|
|
250
|
+
routerInterceptors.beforeResolve.bind(routerInterceptors)
|
|
251
|
+
);
|
|
252
|
+
this._router.afterEach(
|
|
253
|
+
routerInterceptors.afterEach.bind(routerInterceptors)
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @private
|
|
259
|
+
* @memberof Application
|
|
260
|
+
*/
|
|
261
|
+
_initVue() {
|
|
262
|
+
this._app = createApp({
|
|
263
|
+
setup: () => {
|
|
264
|
+
const applicaiton = reactive(this);
|
|
265
|
+
|
|
266
|
+
return () => h(this.options.app || App, { applicaiton });
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
this._plugins.forEach((plugin) => {
|
|
271
|
+
this._app.use(plugin);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* @private
|
|
277
|
+
* @memberof Application
|
|
278
|
+
*/
|
|
279
|
+
_initRouter() {
|
|
280
|
+
if (!this._app) {
|
|
281
|
+
throw new Error('Vue实例不存在');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let history = null;
|
|
285
|
+
|
|
286
|
+
let base = this.options.routeBaseUrl || '/';
|
|
287
|
+
|
|
288
|
+
if (this.global.__POWERED_BY_QIANKUN__ && base === '/') {
|
|
289
|
+
base = `/${this.appName}`;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (this._extendOptions?.base) {
|
|
293
|
+
base = this._extendOptions.base + base;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
switch (this.options.routeMode) {
|
|
297
|
+
case 'hash':
|
|
298
|
+
history = createWebHashHistory(base);
|
|
299
|
+
break;
|
|
300
|
+
case 'memory':
|
|
301
|
+
history = createMemoryHistory(base);
|
|
302
|
+
break;
|
|
303
|
+
default:
|
|
304
|
+
history = createWebHistory(base);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
this._history = history;
|
|
308
|
+
this._router = createRouter({ history, routes: [] });
|
|
309
|
+
|
|
310
|
+
this._setRouteInterceptors();
|
|
311
|
+
|
|
312
|
+
this._app.use(this._router);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* @private
|
|
317
|
+
* @memberof Application
|
|
318
|
+
*/
|
|
319
|
+
async _resolveRoutes() {
|
|
320
|
+
//页面定义
|
|
321
|
+
let pageDefines = await pageDataParser(this.options.pages);
|
|
322
|
+
|
|
323
|
+
if (!this._router) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
pageDefines = pageDefines.filter(
|
|
328
|
+
(pageDefine) =>
|
|
329
|
+
typeof pageDefine.name === 'string' &&
|
|
330
|
+
typeof pageDefine.route === 'string'
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
const routes = pageDefines.map((define) => ({
|
|
334
|
+
name: define.name,
|
|
335
|
+
redirect: define.redirect,
|
|
336
|
+
path: define.route,
|
|
337
|
+
props:
|
|
338
|
+
define.props || ((route) => Object.assign(route.params, route.query)),
|
|
339
|
+
meta: define.meta,
|
|
340
|
+
component: define.component,
|
|
341
|
+
children: [],
|
|
342
|
+
}));
|
|
343
|
+
|
|
344
|
+
const rootRoutes = [];
|
|
345
|
+
|
|
346
|
+
for (let i = 0, length = routes.length; i < length; i++) {
|
|
347
|
+
const pageDefine = pageDefines[i];
|
|
348
|
+
const route = routes[i];
|
|
349
|
+
for (let j = 0; j < length; j++) {
|
|
350
|
+
if (i === j) {
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const innerPageDefine = pageDefines[j];
|
|
354
|
+
const innerRoute = routes[j];
|
|
355
|
+
|
|
356
|
+
if (pageDefine.name == null || innerPageDefine.name == null) {
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (pageDefine.name === innerPageDefine.parent) {
|
|
361
|
+
route.children.push(innerRoute);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (!pageDefine.parent) {
|
|
366
|
+
rootRoutes.push(route);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
for (const rootRoute of rootRoutes) {
|
|
371
|
+
this._router.addRoute(rootRoute);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* @private
|
|
377
|
+
* @memberof Application
|
|
378
|
+
*/
|
|
379
|
+
_initEvent() {
|
|
380
|
+
this.on('subApplicationCreate', (application) => {
|
|
381
|
+
this.subApplications.push(application);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
this.on('subApplicationDestroy', (application) => {
|
|
385
|
+
this.subApplications = this.subApplications.filter(
|
|
386
|
+
(app) => app !== application
|
|
387
|
+
);
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* @private
|
|
393
|
+
* @memberof Application
|
|
394
|
+
*/
|
|
395
|
+
_exportLifecycle() {
|
|
396
|
+
if (!this.global.__POWERED_BY_QIANKUN__ || !this.appName) {
|
|
397
|
+
this._viewportMountPromise = Promise.resolve();
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (!this.global.moudleQiankunAppLifeCycles) {
|
|
402
|
+
this.global.moudleQiankunAppLifeCycles = {};
|
|
403
|
+
}
|
|
404
|
+
this.global.moudleQiankunAppLifeCycles[this.appName] = {
|
|
405
|
+
bootstrap: async (props) => {},
|
|
406
|
+
mount: async ({ ownerApplication, container, extendOptions = {} }) => {
|
|
407
|
+
this.parent = ownerApplication || null;
|
|
408
|
+
|
|
409
|
+
this._extendOptions = extendOptions;
|
|
410
|
+
|
|
411
|
+
this._initVue();
|
|
412
|
+
|
|
413
|
+
this._initRouter();
|
|
414
|
+
|
|
415
|
+
if (ownerApplication) {
|
|
416
|
+
this._iocContainer.setParent(ownerApplication.getIocContainer());
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (this._targetSelector) {
|
|
420
|
+
this._app.mount(container.querySelector(this._targetSelector));
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
this._resolveRoutesPromise = this._resolveRoutes();
|
|
424
|
+
this._resolveRoutesPromise.then(() => {
|
|
425
|
+
this._resolveRoutesPromise = null;
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
if (typeof this._mountResolve === 'function') {
|
|
429
|
+
this._mountResolve();
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.parent?.emit('subApplicationCreate', this);
|
|
433
|
+
},
|
|
434
|
+
unmount: async () => {
|
|
435
|
+
this.parent?.emit('subApplicationDestroy', this);
|
|
436
|
+
this.parent = null;
|
|
437
|
+
this._history.destroy();
|
|
438
|
+
this._app.unmount();
|
|
439
|
+
this._app = null;
|
|
440
|
+
this._router = null;
|
|
441
|
+
this._extendOptions = null;
|
|
442
|
+
this._iocContainer.setParent(null);
|
|
443
|
+
// this._iocContainer.clear();
|
|
444
|
+
if (this._targetSelector) {
|
|
445
|
+
this.unmount();
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
*
|
|
453
|
+
* @param {string} name
|
|
454
|
+
* @return {MicroApplication}
|
|
455
|
+
* @memberof Application
|
|
456
|
+
*/
|
|
457
|
+
getMicroApplication(name) {
|
|
458
|
+
return (
|
|
459
|
+
this.microApplications.find(
|
|
460
|
+
(microApplication) => microApplication.name === name
|
|
461
|
+
) ?? null
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* @param {object} options
|
|
467
|
+
* @memberof Application
|
|
468
|
+
* @returns {MicroApplication}
|
|
469
|
+
*/
|
|
470
|
+
createMicroApplication(options) {
|
|
471
|
+
const microApplication = new MicroApplication(options, this);
|
|
472
|
+
this.microApplications.push(microApplication);
|
|
473
|
+
return microApplication;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* 触发父应用事件
|
|
478
|
+
* @param {string} eventName
|
|
479
|
+
* @param {...any} args
|
|
480
|
+
* @returns
|
|
481
|
+
*/
|
|
482
|
+
emitParentApplication(eventName, ...args) {
|
|
483
|
+
if (!this.parent) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
this.parent.emit('fromSubApplication', this.appName, eventName, ...args);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* 使用插件
|
|
492
|
+
* @param {import('vue').Plugin} plugin
|
|
493
|
+
* @returns {Application}
|
|
494
|
+
*/
|
|
495
|
+
use(plugin) {
|
|
496
|
+
this._plugins.push(plugin);
|
|
497
|
+
// this._app.use(plugin);
|
|
498
|
+
return this;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* @param {string} target
|
|
503
|
+
* @memberof Application
|
|
504
|
+
*/
|
|
505
|
+
async mount(target) {
|
|
506
|
+
if (typeof target !== 'string') {
|
|
507
|
+
throw new Error('mount: 指接受字符串参数。');
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
if (this.global.__POWERED_BY_QIANKUN__) {
|
|
511
|
+
const mountPromise = new Promise((resolve) => {
|
|
512
|
+
this._mountResolve = resolve;
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
this._targetSelector = target;
|
|
516
|
+
|
|
517
|
+
await mountPromise;
|
|
518
|
+
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
this._initVue();
|
|
523
|
+
this._initRouter();
|
|
524
|
+
this._app.mount(target);
|
|
525
|
+
|
|
526
|
+
this._resolveRoutesPromise = this._resolveRoutes();
|
|
527
|
+
this._resolveRoutesPromise.then(() => {
|
|
528
|
+
this._resolveRoutesPromise = null;
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* @memberof Application
|
|
534
|
+
*/
|
|
535
|
+
unmount() {
|
|
536
|
+
this._app?.unmount();
|
|
537
|
+
}
|
|
538
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { loadMicroApp } from 'qiankun';
|
|
2
|
+
|
|
3
|
+
export class MicroApplication {
|
|
4
|
+
constructor(options, application) {
|
|
5
|
+
options = options || {};
|
|
6
|
+
this.name = options.name;
|
|
7
|
+
this.entry = options.entry;
|
|
8
|
+
|
|
9
|
+
this.extendOptions = options?.extendOptions || {};
|
|
10
|
+
|
|
11
|
+
this.ownerApplication = application;
|
|
12
|
+
|
|
13
|
+
this._microApp = null;
|
|
14
|
+
this.container = null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @private
|
|
19
|
+
* @return {import('qiankun').MicroApp}
|
|
20
|
+
* @memberof MicroApplication
|
|
21
|
+
*/
|
|
22
|
+
_loadMicroApp() {
|
|
23
|
+
return loadMicroApp({
|
|
24
|
+
name: this.name,
|
|
25
|
+
entry: this.entry,
|
|
26
|
+
container: this.container,
|
|
27
|
+
props: {
|
|
28
|
+
// ...this.props,
|
|
29
|
+
extendOptions: this.extendOptions,
|
|
30
|
+
ownerApplication: this.ownerApplication,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @returns {Promise<void>}
|
|
37
|
+
* @memberof MicroApplication
|
|
38
|
+
*/
|
|
39
|
+
async mountApplication() {
|
|
40
|
+
if (this._microApp?.getStatus() === 'UNMOUNTING') {
|
|
41
|
+
await this._microApp.unmountPromise();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!this._microApp) {
|
|
45
|
+
this.container = document.createElement('div');
|
|
46
|
+
this._microApp = this._loadMicroApp();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
await this._microApp.mountPromise;
|
|
50
|
+
|
|
51
|
+
if (this._microApp.getStatus() === 'NOT_MOUNTED') {
|
|
52
|
+
await this._microApp.mount();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async unmountApplication() {
|
|
57
|
+
if (!this._microApp) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (this._microApp.getStatus() !== 'MOUNTED') {
|
|
62
|
+
await this._microApp.mountPromise;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
await this._microApp.unmount();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<BwaApplication :application="applicaiton">
|
|
3
|
+
<Suspense>
|
|
4
|
+
<RouterView />
|
|
5
|
+
</Suspense>
|
|
6
|
+
</BwaApplication>
|
|
7
|
+
</template>
|
|
8
|
+
|
|
9
|
+
<script setup>
|
|
10
|
+
import { toRefs } from 'vue';
|
|
11
|
+
import BwaApplication from './application.vue';
|
|
12
|
+
import { RouterView } from 'vue-router';
|
|
13
|
+
|
|
14
|
+
const props = defineProps({
|
|
15
|
+
applicaiton: {
|
|
16
|
+
Object,
|
|
17
|
+
required: true,
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const { applicaiton } = toRefs(props);
|
|
22
|
+
</script>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div :class="ns.b()">
|
|
3
|
+
<slot></slot>
|
|
4
|
+
</div>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script setup>
|
|
8
|
+
import { applicationKey } from '@ctzy-web-client/tokens';
|
|
9
|
+
import { provide, ref, watch } from 'vue';
|
|
10
|
+
import { useRoute } from 'vue-router';
|
|
11
|
+
import { useNamespace, provideGlobalConfig } from '../hooks';
|
|
12
|
+
|
|
13
|
+
defineOptions({
|
|
14
|
+
name: 'BwaApplication',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const props = defineProps({
|
|
18
|
+
application: {
|
|
19
|
+
type: Object,
|
|
20
|
+
required: true,
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const ns = useNamespace('application');
|
|
25
|
+
|
|
26
|
+
watch(
|
|
27
|
+
useRoute(),
|
|
28
|
+
(currentRoute) => {
|
|
29
|
+
props.application.emitParentApplication('route-change', currentRoute);
|
|
30
|
+
},
|
|
31
|
+
{ immediate: true }
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
provide(applicationKey, ref(props.application));
|
|
35
|
+
provideGlobalConfig({ [applicationKey]: props.application }, null, true);
|
|
36
|
+
</script>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { withInstall } from 'element-plus/es/utils/vue/install';
|
|
2
|
+
import Application from './application.vue';
|
|
3
|
+
import MicroApplication from './micro-application.vue';
|
|
4
|
+
|
|
5
|
+
export { Application } from './Application';
|
|
6
|
+
|
|
7
|
+
export const BwaApplication = withInstall(Application, { MicroApplication });
|
|
8
|
+
|
|
9
|
+
export const BwaMicroApplication = withInstall(MicroApplication);
|
|
10
|
+
|
|
11
|
+
export default BwaApplication;
|