将选项传递给 ES6 模块导入 [英] Pass options to ES6 module imports

查看:22
本文介绍了将选项传递给 ES6 模块导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将选项传递给 ES6 导入?

Is it possible to pass options to ES6 imports?

你如何翻译:

var x = require('module')(someoptions);

到 ES6?

推荐答案

单个 import 语句无法做到这一点,它不允许调用.

There is no way to do this with a single import statement, it does not allow for invocations.

所以你不会直接调用它,但你基本上可以像 commonjs 对默认导出所做的一样:

So you wouldn't call it directly, but you can basically do just the same what commonjs does with default exports:

// module.js
export default function(options) {
    return {
        // actual module
    }
}

// main.js
import m from 'module';
var x = m(someoptions);

或者,如果您使用支持 monadic 承诺的模块加载器,您可能会能够做类似的事情

Alternatively, if you use a module loader that supports monadic promises, you might be able to do something like

System.import('module').ap(someoptions).then(function(x) {
    …
});

使用新的 import 运算符成为

With the new import operator it might become

const promise = import('module').then(m => m(someoptions));

const x = (await import('module'))(someoptions)

但是,您可能不需要动态导入,而是需要静态导入.

however you probably don't want a dynamic import but a static one.

这篇关于将选项传递给 ES6 模块导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆