生成es6模块导出 [英] Generating es6 module exports

查看:110
本文介绍了生成es6模块导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式生成模块的导出,是否可能在es6中?

I'm wanting to programmatically generate the exports for a module, is this possible in es6?

以下这些行:

const ids = ['foo', 'bar', 'baz'];

ids.forEach(id => {
    export const [id.toUpperCase()] = id;
});


推荐答案

不,不是。出口和进口需要在ES6模块中静态分析。

No, it's not. Exports and imports are required to be statically analysable in ES6 modules.

不仅没有顶级导出声明语法错误,而且您尝试使用动态名称声明变量。括号符号仅用于计算的属性

Not only is that no-top-level export declaration a syntax error, but also your attempt at declaring variables with dynamic names. The bracket notation is reserved for computed properties only.

因此,如果要以编程方式生成模块导出,则需要动态生成模块源文本(作为构建过程的一部分)。

So if you are going to programmatically generate module exports, you'll need to dynamically generate the module source text (as a part of your build process).

这篇关于生成es6模块导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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