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

查看:104
本文介绍了将选项传递给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 promises,你可以做一些像

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) {
    …
});

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

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