gpt4 book ai didi

javascript - 带有常量路径变量的reactjs动态导入

转载 作者:行者123 更新时间:2023-12-03 14:31:54 24 4
gpt4 key购买 nike

我在使用reactjs动态导入时遇到了一个奇怪的问题。假设我有一个组件,其名称为 ComponentA,其路径类似于 myComponents/ComponentA。现在,当我像下面的代码一样动态导入它时,它会很好地工作:

Promise.all(
[
import('myComponents/ComponentA'),
// other imports
]
).then(....);

但是如果我在常量变量中定义组件路径,如下所示:

//before definition of my current component
const PATH = 'myComponents/ComponentA';
.
.
.
// some where in my component class
Promise.all(
[
import(PATH),
// other imports
]
).then(....);

它会给我一个这样的错误:

Error: Cannot find module 'myComponents/ComponentA'.

有时,如果我只是向 PATH 变量添加一个空字符串,就可以解决问题,但有时却不能。

//before definition of my current component
const PATH = 'myComponents/ComponentA';
.
.
.
// some where in my component class
Promise.all(
[
import(''+PATH), // some times by adding empty string, problem would be solved
// other imports
]
).then(....);

任何有关正在发生的事情的想法将不胜感激。

最佳答案

也许可以尝试这个新的 ES6 语法:

const PATH = 'myComponents/ComponentA';
Promise.all(
[
import(`${PATH}`), // try pass it like this
// other imports
]
).then(....);

关于javascript - 带有常量路径变量的reactjs动态导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48042088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com