ES6 模块语法:是否可以将 * 作为 Name from ... 导出? [英] ES6 module syntax: is it possible to `export * as Name from ...`?

查看:23
本文介绍了ES6 模块语法:是否可以将 * 作为 Name from ... 导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

见问题标题.我找到了一个很好的参考,用于export的形式,但我还没有看到我正在寻找.

See question title. I found a great reference for the forms of export available, but I have not seen what I'm looking for.

是否可以执行以下操作?

Is it possible to do something like the following?

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

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

即这将在 index.js 内提供一个命名导出 Constants,其中包含来自 constants.js 的所有命名导出.

I.e. this would provide a named export Constants inside of index.js containing all of the named exports from constants.js.

这个答案似乎表明它不是可能在 TypeScript 中;纯 JavaScript 也是一样吗?

This answer seems to indicate it's not possible in TypeScript; is the same true for pure JavaScript?

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

(This example is a bit contrived; in reality I'm trying to have a prop-types.js module that uses named exports for internal use within the React package, but also exports the prop type definitions under PropTypes for external consumption. I tried to simplify for the sake of the question.)

推荐答案

不,在 JS 中也是不允许的,但是有 添加它的提议.现在,只需使用导入本地变量并导出的两步过程:

No, it's not allowed in JS either, however there is a proposal to add it. For now, just use the two-step process with importing into a local variable and exporting that:

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

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

这篇关于ES6 模块语法:是否可以将 * 作为 Name from ... 导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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