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

问题描述

查看问题标题。我找到了,可以获得 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';

I.e。这将在 index.js 内提供一个名为export Constants ,其中包含来自 module.js 。

I.e. this would provide a named export Constants inside of index.js containing all of the named exports from module.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包中使用命名导出供内部使用,但也导出 PropTypes 下的prop类型定义以供外部使用。我试过为了问题而简化。)

(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模块语法:是否可以`从...导出*作为名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:59