查看问题标题。我找到了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类型定义以供外部使用。我试图简化为问题的缘故。)
不,它也不允许在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};
// 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';