Quellcode durchsuchen

1.0.49 恢复 JsUtil

杨志杰 vor 3 Jahren
Ursprung
Commit
3b14ce9c0b
3 geänderte Dateien mit 31 neuen und 1 gelöschten Zeilen
  1. 1 1
      package.json
  2. 28 0
      packages/common/JsUtil.js
  3. 2 0
      packages/components.js

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "pc-component-v3",
-  "version": "1.0.48",
+  "version": "1.0.49",
   "description": "",
   "main": "dist/pc-component-v3.js",
   "scripts": {

+ 28 - 0
packages/common/JsUtil.js

@@ -0,0 +1,28 @@
+/**
+ * 动态加载js模块
+ * @param {*} jsUrl js文件的路径
+ * @return promise对象
+ */
+let dynamicLoadJsModule = function(jsUrl){
+  let promise = null;
+    
+  // 注意:在webpack中直接使用import(url).then()会错误的解析url。
+  // 如果url是静态的值,举例'xxx.js', webpack打包的时候,会去src对应的目录下面找js文件。
+  // 如果url是变量,举例const url = '', webpack打包的时候,原理不清晰...,测试结果时浏览器不会发出http请求加载后端的js文件。
+  let command = `
+        promise = new Promise((resolve, reject) => {
+            import('${jsUrl}').then(remoteComponent => {
+                resolve(remoteComponent);
+            }).catch(error => {
+                reject(error);
+            });
+        });
+    `;
+  eval(command);
+  return promise;
+};
+
+
+export default {
+  dynamicLoadJsModule,
+};

+ 2 - 0
packages/components.js

@@ -42,6 +42,7 @@ import UserStorageResource from './common/UserStorageResource.js';
 import DownloadService from './common/DownloadService.js';
 import ProcessReportResource from './process/src/api/ProcessReportResource.js';
 import CssUtil from './common/CssUtil.js';
+import JsUtil from './common/JsUtil.js';
 
 import ModalFix from './modal/src/ModalFix.js';
 import PrintUtil from './print/src/PrintUtil.js';
@@ -88,4 +89,5 @@ export {
   DownloadService,
   ProcessReportResource,
   CssUtil,
+  JsUtil,
 };