且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何有条件地导入 ES6 模块?

更新时间:2022-12-17 11:44:57

我们现在有了 ECMA 的动态导入提案.这是在第 3 阶段.这也可用作 babel-preset.

We do have dynamic imports proposal now with ECMA. This is in stage 3. This is also available as babel-preset.

以下是根据您的情况进行条件渲染的方法.

Following is way to do conditional rendering as per your case.

if (condition) {
    import('something')
    .then((something) => {
       console.log(something.something);
    });
}

这基本上返回了一个承诺.承诺的决议有望有模块.该提案还具有其他功能,例如多个动态导入、默认导入、js 文件导入等.您可以找到有关 这里是动态导入.

This basically returns a promise. Resolution of promise is expected to have the module. The proposal also have other features like multiple dynamic imports, default imports, js file import etc. You can find more information about dynamic imports here.