Skip to content

Commit fb57208

Browse files
committed
feat(module-loader): 模块在 Vue 实例化之前调用方法
affects: @vue-async/module-loader-typing, @vue-async/module-loader 添加 Load() 方法在 Vue 实例化之前加载子模块 umd,iife打包文件打包已经包含utils代码,并通过 babel 编译 移除build commonjs min文件
1 parent 2dbc379 commit fb57208

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+6082
-360
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# @vue-async/module-loader-type
1+
# @vue-async/module-loader-typing
22

3-
### TypeScript definitions for @vue-async/module-loader but using in the child module
3+
### TypeScript definitions for @vue-async/module-loader but using in the child modules
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import './vue';
22

3-
export { Framework, DynamicComponent } from './module';
3+
export { Framework, DynamicComponent, ModuleContext } from './module';

packages/module-loader-typing/module.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ export type DynamicComponent =
66
| AsyncComponent
77
| ({ component: VueComponent | AsyncComponent; name?: string } & Record<string, any>);
88

9+
export interface ModuleContext {
10+
$componentLoader: (componentName: string, path: string) => Promise<VueComponent>;
11+
$dynamicComponent?: {
12+
add: (component: DynamicComponent, position?: string) => void;
13+
remove: (name: string, position?: string) => void;
14+
};
15+
$eventBus: {
16+
emit: (eventName: string, playload: any) => void;
17+
on: (eventName: string, handler: (playload: any) => void) => void;
18+
off: (eventName: string, handler: (playload: any) => void) => void;
19+
clear: () => void;
20+
getEvents: () => Record<string, any>;
21+
};
22+
}
23+
924
export interface Framework {
1025
readonly layouts: Record<string, VueComponent | AsyncComponent>;
1126
addRouters: (routes: RouteConfig[]) => void; // 可以被重写
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
import { Framework, DynamicComponent } from './module';
1+
import { Framework, ModuleContext } from './module';
22

33
declare module 'vue/types/vue' {
4-
interface Vue {
5-
// @ts-ignore
4+
interface Vue extends ModuleContext {
65
$moduleLoadManager: Framework & Record<string, any>;
7-
$dynamicComponent: {
8-
add: (component: DynamicComponent, position?: string) => void;
9-
remove: (name: string, position?: string) => void;
10-
};
11-
$eventBus: {
12-
emit: (eventName: string, playload: any) => void;
13-
on: (eventName: string, handler: (playload: any) => void) => void;
14-
off: (eventName: string, handler: (playload: any) => void) => void;
15-
clear: () => void;
16-
getEvents: () => Record<string, any>;
17-
};
186
}
197
}

packages/module-loader/README.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,160 @@
11
# @vue-async/module-loader
2+
3+
从远程地址链接加载`模块`.
4+
<br>
5+
6+
```js
7+
// ## 主程序 ##
8+
9+
// 引入模块
10+
import ModuleLoader from '@vue-async/module-loader';
11+
12+
Vue.use(ModuleLoader);
13+
14+
// 方法一
15+
var moduleLoader = new ModuleLoader({});
16+
17+
var app = new Vue({
18+
moduleLoader,
19+
render:(h)=> h(App)
20+
})
21+
22+
app.$moduleLoader(ModuleConfig).then(()=>{
23+
app.$mount('#app')
24+
})
25+
26+
// 方法二 (例如在 nuxtjs 的 plugin 中使用时)
27+
var plugin = (context) => {
28+
// 这里需要手动注册 dynamicComponent,
29+
// 子模块中才可以能过 this.$dynamicComponent.add()方法
30+
var moduleLoader =
31+
new ModuleLoader({}).registerDynamicComponent(context.store);
32+
33+
await moduleLoader.load(ModuleConfig)
34+
35+
// 添加到 Vue options 中
36+
context.app.moduleLoader = moduleLoader
37+
}
38+
export default plugin
39+
```
40+
``` js
41+
// ## 子模块 ##
42+
// index.js 入口文件
43+
export default function(Vue, options){}
44+
45+
// Typescript
46+
import { VueConstructor } from 'vue';
47+
import { ModuleContext } from '@vue-async/module-loader-typing';
48+
49+
// 方法一调用方式
50+
export default function(this:InstanceType<VueConstructor>, Vue:VueConstructor, options: Recode<string, any> ={}){}
51+
52+
// 方法二调用方式
53+
export default function(this:ModuleContext, Vue:VueConstructor, options: Recode<string, any> ={}){}
54+
```
55+
56+
## ModuleConfig 模块配置
57+
58+
示例:
59+
``` js
60+
// 数组配置方式(支持与对象配置方式混合)
61+
[
62+
// Key/Value
63+
{
64+
// 模块名称:远程 umd 文件 url 地址
65+
moduleName: 'remote url',
66+
//
67+
moduleName: {
68+
entry: 'remote url', // 远程 umd 文件 url 地址
69+
styles: [],
70+
args:{}
71+
}
72+
},
73+
// Object
74+
{
75+
moduleName: '',
76+
entry: '',
77+
styles:[]
78+
},
79+
// Function
80+
function entry(Vue){}
81+
]
82+
83+
// or 对象配置方式
84+
{
85+
moduleName: 'remote url',
86+
moduleName: {
87+
entry: 'remote url',
88+
styles: [],
89+
args:{}
90+
},
91+
function entry(Vue){}
92+
}
93+
```
94+
`moduleName`
95+
Type: `string`
96+
umd 文件时打包时 lib 名, 必填项。
97+
98+
99+
`entry`
100+
Type: `string`
101+
远程 umd 文件 url 地址, 必填项
102+
103+
`styles`
104+
Type: `string` | `string[]`
105+
如果打包时样式文件被分离,可以能过这里引用,可选项
106+
107+
`args`
108+
Type: `Object`
109+
参数将会传递到入口方法中的第二个参数,可选项(子模块需要判断 `undefined`
110+
此参数与 `$moduleLoaderManager` 区别在于此参数只会传递给单个子模块调用
111+
<br>
112+
113+
## 子模块上下文方法
114+
`this.$componentLoader(remote url)`
115+
远程加载单个组件,返回 `Promise<VueComponent>`, 可作为 Vue 异步组件直接加载。
116+
<br>
117+
118+
`this.$eventBus`
119+
父子模块通讯
120+
121+
&nbsp;&nbsp;&nbsp; 方法:
122+
&nbsp;&nbsp;&nbsp; `emit(eventName,playload)` 触发事件
123+
&nbsp;&nbsp;&nbsp; `on(eventName, (playload)=>{})` 添加事件
124+
&nbsp;&nbsp;&nbsp; `off(eventName, (playload)=>{})` 移除事件
125+
&nbsp;&nbsp;&nbsp; `clean()` 移除所有事件
126+
&nbsp;&nbsp;&nbsp; `getEvents()` 获取所有事件
127+
<br>
128+
129+
`this.$dynamicComponent`
130+
子模块注册动态组件到主程序 `store` namespace `dynamicComponent`
131+
当使用方法一在 `new Vue({})` 之后调用时,如果引用了 `vuex` 将会自动注入此方法
132+
当使用方法二在之前调用时,需要手动 `registerDynamicComponent(store)` 之后才可以被调用
133+
134+
&nbsp;&nbsp;&nbsp; 方法:
135+
&nbsp;&nbsp;&nbsp; `add(component, position)` 添加组件到指定的位置
136+
&nbsp;&nbsp;&nbsp; `remove(name, position)` 从指定位置移除指定名字组件
137+
&nbsp;&nbsp;&nbsp; 注意:
138+
&nbsp;&nbsp;&nbsp; `component`: 需要配置 `name` 属性 或 通过 `{ name: '', component: Compnent }` 自定义名称,移出时通过 name 移除
139+
&nbsp;&nbsp;&nbsp; `position` 可选项, 默认值:'GLOBAL'
140+
<br>
141+
142+
`this.$moduleLoadManager`
143+
实例化 `ModuleLoader` 时的参数能过合并后的值, 支持 Vue 响应。
144+
注:在 Vue 被实例化之前加载子模块时,入口函数内无法调用到此对象
145+
默认值:
146+
`layouts`
147+
Type: `Object`
148+
模板对象
149+
150+
`addLayouts(name:string, layout: VueComponent)`
151+
将组件模板添加到 `layouts` 中, 此方法可以在实例化参数中被覆盖重写
152+
153+
`addRoutes(routes:RouteConfig[])`
154+
添加路由, 主程序需要引用`vue-router`, 此方法可以在实例化参数中被覆盖重写
155+
156+
注:当主组件引用`vue-router`包后,将会自动注入方法解决404问题,需要在主程序中添加一条配置 `name``404` `page-not-found` `not-found``path``*` 的路由后生效
157+
158+
159+
160+

0 commit comments

Comments
 (0)