是否可以将"import * as"的结果导出到在ES2015中? [英] Is it possible to export the result of "import * as" in ES2015?

查看:130
本文介绍了是否可以将"import * as"的结果导出到在ES2015中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ES2015中,可以将整个模块作为对象导入,其属性是模块的导出:

In ES2015, it's possible to import an entire module as an object whose properties are the module's exports:

import * as name from 'module';

我发现这对于命名空间非常有用,并一直使用.

I find this to be extraordinarily useful for namespacing and use it all the time.

还可以重新导出其他模块的导出:

It's also possible to re-export other modules' exports:

export { name } from 'module'; // selectively
export * from 'other-module'; // indiscriminately

现在,我正在尝试使用这种样式的命名空间编写一个库.收集顶层模块中所有内容的直观方式如下:

Now I'm trying to write a library with namespacing in this style. The intuitive way of collecting everything in the top-level module would be like this:

export * as name from 'module';

但这似乎不起作用; Babel和Rollup都拒绝了.

But that doesn't seem to work; Babel and Rollup both reject it.

可以将模块作为对象导入,通过对其键进行迭代来创建克隆,然后导出该克隆,但是那样的话,它将只是一个普通的旧动态对象,因此我将失去很多汇总提供的优势.

I could import the module as an object, create a clone by iterating over its keys, and export that, but then it would just be a plain old dynamic object, so I would lose the great advantages Rollup provides.

那么,有没有真的用声明性模块语法来做到这一点?在我看来,这没有任何借口.

So, is there really no way to do this with the declarative module syntax? It seems to me like there's no excuse for that.

推荐答案

不,这在ES6中只是被忽略了.不过,有是第1阶段的提案,用于添加这些提案,并且汇总将考虑实施.

No, this was simply missed in ES6. There is a stage 1 proposal to add these, though, and rollup will consider implementing it.

直到那时,您将需要使用两个声明和一个本地绑定,尽管完全不需要克隆对象:

Until then, you will need to use two declarations and a local binding, altough there's no need to clone the object:

import * as name from 'module';
export { name };

这篇关于是否可以将"import * as"的结果导出到在ES2015中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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