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

问题描述 投票:9回答:2

查看问题标题。我找到了great reference可用的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的所有命名导出。


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

(这个例子有点做作;实际上我正在尝试使用一个prop-types.js模块,它在React包中使用命名导出用于内部使用,但也导出PropTypes下的prop类型定义以供外部使用。我试图简化为问题的缘故。)

javascript ecmascript-6
2个回答
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};

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';
© www.soinside.com 2019 - 2024. All rights reserved.