[英]Dynamic import in different folder
I have some trouble to import dynamicaly a class. 我很难动态导入一个类。
I use alias for this projet : 我为此别名使用别名:
config.resolve.alias = {
App: path.resolve('./src/'),
Reactive: path.resolve('./app/')
}
I want to import a list of class : 我想导入课程列表:
const classes = {
foo: 'App/Foo',
bar: 'App/Bar'
};
let list = {};
for(var c in classes) {
(async (k, v, list) => {
const m = await import(`${v}`);
list[k] = new m.default();
})(c, classes[c], list);
}
This script is called in app
, and all imported classes in src
. 此脚本在
app
调用,所有导入的类在src
调用。
The error is simple : Cannot find module 'App/Foo'
. 错误很简单:
Cannot find module 'App/Foo'
。
When I check the last entry of error's log : 当我检查错误日志的最后一个条目时:
var map = {
"./OtherClass1": [
"./app/OtherClass1.js"
],
"./OtherClass1.js": [
"./app/OtherClass1.js"
],
"./OtherClass2": [
"./app/OtherClass2.js"
],
"./OtherClass2.js": [
"./app/OtherClass2.js"
]
};
function webpackAsyncContext(req) {
var ids = map[req];
if(!ids)
return Promise.reject(new Error("Cannot find module '" + req + "'."));
return Promise.all(ids.slice(1).map(__webpack_require__.e)).then(function() {
return __webpack_require__(ids[0]);
});
};
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
return Object.keys(map);
};
webpackAsyncContext.id = "./app lazy recursive ^.*$";
module.exports = webpackAsyncContext;
So, the error is legit, because the map
does not contain Foo
and Bar
classes in src
, only those in app
. 因此,错误是合法的,因为
map
不包含src
Foo
和Bar
类,仅包含app
Foo
和Bar
类。
How can I specify to Webpack to check in both folders, recursively? 如何指定Webpack递归检入两个文件夹?
But, when I test this, it's work fine : 但是,当我对此进行测试时,效果很好:
for(var c in classes) {
(async (k, v, list) => {
const m = await import(`${"App/Foo"}`);
list[k] = new m.default();
})(c, classes[c], list);
}
use react import to import your file and use file.classname
to call them 使用react import导入文件并使用
file.classname
调用它们
eg import claases from '/src';
例如
import claases from '/src';
and use it link 并使用它的链接
app = classes.myfile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.