我需要做类似的事情:
if (condition) {
import something from 'something';
}
// ...
if (something) {
something.doStuff();
}
上面的代码无法编译;它抛出SyntaxError: ... 'import' and 'export' may only appear at the top level
。
我尝试使用此处System.import
所示的方法,但是我不知道从哪里来。这是没有最终被接受的ES6提案吗?该文章中指向“程序化API”的链接将我转至不推荐使用的docs页面。System
现在,ECMA确实有动态进口建议。这是在第3阶段。这也可以作为babel-preset使用。
以下是根据您的情况进行条件渲染的方法。
if (condition) {
import('something')
.then((something) => {
console.log(something.something);
});
}
这基本上返回了一个承诺。承诺解决方案有望包含该模块。该提案还具有其他功能,例如多个动态导入,默认导入,js文件导入等。您可以在此处找到有关动态导入的更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句