Skip to content

Commit 385b7cc

Browse files
committed
merge
2 parents beba540 + ceb65b2 commit 385b7cc

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-module-loader",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Let you use the micro front-end architecture to build Vue applications.",
55
"author": "mqhe2007 <122274389@qq.com>",
66
"homepage": "https://mengqinghe.com",

src/ability/moduleLoader.js

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
import { print } from '../tool/index'
2-
export default function(moduleData) {
3-
if (typeof moduleData === 'object') {
4-
/** 通过模块清单加载模块 */
5-
let promiseAll = []
6-
for (let moduleName in moduleData) {
7-
if (!window[moduleName]) {
8-
promiseAll.push(
9-
new Promise((resolve, reject) => {
10-
let script = document.createElement('script')
11-
script.src = moduleData[moduleName]
12-
script.onload = () => {
13-
if (window[moduleName]) {
14-
typeof window[moduleName] === 'function'
15-
? window[moduleName].call(this)
16-
: window[moduleName].default.call(this)
17-
print(moduleName, '模块加载完毕')
18-
} else {
19-
console.warn(
20-
moduleName,
21-
'模块加载失败,请检查模块资源是否成功加载。已跳过。'
22-
)
2+
export default function(Vue) {
3+
return function(moduleData) {
4+
if (typeof moduleData === 'object') {
5+
/** 通过模块清单加载模块 */
6+
let promiseAll = []
7+
for (let moduleName in moduleData) {
8+
if (!window[moduleName]) {
9+
promiseAll.push(
10+
new Promise((resolve, reject) => {
11+
let script = document.createElement('script')
12+
script.src = moduleData[moduleName]
13+
script.onload = () => {
14+
if (window[moduleName]) {
15+
typeof window[moduleName] === 'function'
16+
? window[moduleName].call(this, Vue)
17+
: window[moduleName].default.call(this, Vue)
18+
print(moduleName, '模块加载完毕')
19+
} else {
20+
console.warn(
21+
moduleName,
22+
'模块加载失败,请检查模块资源是否成功加载。已跳过。'
23+
)
24+
}
25+
resolve()
2326
}
24-
resolve()
25-
}
26-
script.onerror = () => {
27-
console.warn(moduleName, ': 创建模块脚本元素失败。已跳过。')
28-
resolve()
29-
}
30-
document.body.appendChild(script)
31-
})
32-
)
27+
script.onerror = () => {
28+
console.warn(moduleName, ': 创建模块脚本元素失败。已跳过。')
29+
resolve()
30+
}
31+
document.body.appendChild(script)
32+
})
33+
)
34+
}
3335
}
36+
return Promise.all(promiseAll)
37+
} else if (typeof moduleData === 'function') {
38+
/** 通过模块函数加载模块 */
39+
moduleData.call(this, Vue)
40+
} else {
41+
console.error('模块加载方法只接受模块列表对象或者模块函数对象作为参数。')
3442
}
35-
return Promise.all(promiseAll)
36-
} else if (typeof moduleData === 'function') {
37-
/** 通过模块函数加载模块 */
38-
moduleData.call(this)
39-
} else {
40-
console.error('模块加载方法只接受模块列表对象或者模块函数对象作为参数。')
4143
}
4244
}

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import eventBus from './ability/eventBus'
44
import moduleLoader from './ability/moduleLoader'
55
export default function(Vue, options = {}) {
66
Vue.prototype.$eventBus = eventBus()
7-
Vue.prototype.$moduleLoader = moduleLoader
7+
Vue.prototype.$moduleLoader = moduleLoader(Vue)
88
if (options.store) {
99
options.store.registerModule('dynamicComponent', dynamicComponentState)
1010
Vue.prototype.$dynamicComponent = dynamicComponent(options.store)

0 commit comments

Comments
 (0)