|
1 | 1 | 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() |
23 | 26 | } |
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 | + } |
33 | 35 | } |
| 36 | + return Promise.all(promiseAll) |
| 37 | + } else if (typeof moduleData === 'function') { |
| 38 | + /** 通过模块函数加载模块 */ |
| 39 | + moduleData.call(this, Vue) |
| 40 | + } else { |
| 41 | + console.error('模块加载方法只接受模块列表对象或者模块函数对象作为参数。') |
34 | 42 | } |
35 | | - return Promise.all(promiseAll) |
36 | | - } else if (typeof moduleData === 'function') { |
37 | | - /** 通过模块函数加载模块 */ |
38 | | - moduleData.call(this) |
39 | | - } else { |
40 | | - console.error('模块加载方法只接受模块列表对象或者模块函数对象作为参数。') |
41 | 43 | } |
42 | 44 | } |
0 commit comments