ES6模块语法:是否可以`从...导出*作为名称?

时间:2017-06-19 22:05:05

标签: javascript ecmascript-6

查看问题标题。我找到export // file: constants.js export const SomeConstant1 = 'yay'; export const SomeConstant2 = 'yayayaya'; // file: index.js export * as Constants from './constants.js'; 形式的Constants,但我还没有看到我正在寻找的内容。

是否可以执行以下操作?

index.js

即。这将在module.js内提供一个命名导出prop-types.js,其中包含PropTypes的所有已命名导出。

great reference似乎表明它在TypeScript中是不可能的;纯JavaScript是一样的吗?

(这个例子有点做作;实际上我正在尝试使用一个.mobileNav模块,它在React包中使用命名导出供内部使用,但也导出{{1}下的prop类型定义对于外部消费。我试图为了问题而简化。)

4 个答案:

答案 0 :(得分:17)

不,在JS中也不允许,但是a proposal to add it。现在,只需使用两步流程导入局部变量并导出:

// file: constants.js
export const SomeConstant1 = 'yay';
export const SomeConstant2 = 'yayayaya';

// file: index.js
import * as Constants from './constants.js';
export {Constants};

答案 1 :(得分:2)

此规范的proposal已合并到ecma262。如果您正在运行以前的JS的环境中寻找此功能,则可以使用babel plugin!配置插件后(或者如果您使用的是ecma262或更高版本),则可以在问题中运行JS:

// file: constants.js
export const SomeConstant1 = 'yay';
export const SomeConstant2 = 'yayayaya';

// file: index.js
export * as Constants from './constants.js';

// file: component.js
import { Constants } from './index.js';

const newVar = Constants.SomeConstant1; // 'yay'

答案 2 :(得分:1)

今天是it is now possible,2019年。

export * as name1 from …;

答案 3 :(得分:0)

// file: index.js
// note, this doesn't have to be at the top, you can put it wherever you prefer
import * as AllExportsFromThisModule from "./index.js"; // point this at the SAME file
export default AllExportsFromThisModule;

export const SOME_CONSTANT = 'yay';
export const SOME_OTHER_CONSTANT = 'yayayaya';
相关问题