简体   繁体   English

如何使用Javascript ES6 ES2015模块将常量直接导出/导入到导入模块名称空间?

[英]How do I use Javascript ES6 ES2015 modules to export / import constants directly to the import module namespace?

OK, I can't quite find the answer to this. 好的,我找不到完全的答案。 I am using webpack and babel es2015 preset to handle ES2015 modules. 我正在使用webpack和babel es2015预设来处理ES2015模块。

Module 1 to export, filename "foobar.js" 导出的模块1,文件名“ foobar.js”

export const FOO = 'foo'
export const BAR = 'bar'

Is there a way to import this constant into my global namespace in my import module? 有没有办法将此常量导入到我的导入模块中的全局名称空间中?

I want to do this in my module that will use the constants: 我想在将使用常量的模块中执行此操作:

import 'foobar'

const doSomething = () => { console.log(FOO + BAR) }

I know that this would work: 我知道这会起作用:

import * as CONSTANTS from 'foobar'

const doSomething = () => { console.log(CONSTANTS.FOO + CONSTANTS.BAR) }

...and that there are other ways to achieve the same result of the import being in a particular namespace. ...还有其他方法可以将导入到特定名称空间中的结果相同。 But I want to use the constants without a prefix. 但是我想使用没有前缀的常量。

Is there a way to directly import ALL the exports from a different module into the root namespace of the importing module? 有没有一种方法可以将所有来自不同模块的导出直接导入到导入模块的根名称空间中?

*NOTE: I know that I can do this: *注意:我知道我可以这样做:

import {FOO, BAR} from 'foobar'

But then I have to explicitly reference each of the constants in the import, which leads to more headaches rather than less. 但是然后我必须在导入中显式引用每个常量,这导致更多的麻烦而不是更少的麻烦。

In addition to the two options you already referenced: 除了已经引用的两个选项之外:

import * as CONSTANTS from 'foobar';
import { FOO, BAR } from 'foobar';

You can also do a default export to get them all, but it's functionally the same as option one: 您还可以执行默认导出以全部获取它们,但功能上与选项一相同:

// foobar.js
const FOO = 'foo';
const BAR = 'bar';

export default { FOO, BAR };

// myfile.js
import CONSTANTS from 'foobar';

The other method would be to dynamically populate them for either global or window ( global for Node.js, window for browser code). 另一种方法是动态填充他们无论是global还是windowglobal为Node.js的, window的浏览器代码)。

However, I highly recommend NOT using this method as you won't be able to use any help from your IDE, which will lead to lots of errors. 但是,我强烈建议不要使用此方法,因为您将无法使用IDE的任何帮助,这会导致很多错误。 Writing the extra code now is totally worth not having difficult-to-spot bugs later on. 现在编写额外的代码绝对不值得以后再发现难以发现的错误。

import CONSTANTS from 'foobar';

Object.keys(CONSTANTS).forEach(key => window[key] = CONSTANTS[key]);

console.log(FOO, BAR);   

Example: 例:

 const CONSTANTS = { FOO: 'foo', BAR: 'bar' }; Object.keys(CONSTANTS).forEach(key => window[key] = CONSTANTS[key]); console.log(FOO, BAR); 

By putting them in the root level element, you can get away without having to implement them directly. 通过将它们放在根级别元素中,您可以不必直接实现它们就可以逃脱。

However, please don't use this method. 但是,请不要使用此方法。

There's no way with ES2015 to wildcard import variables into the root namespace of a module. ES2015无法将通配符导入变量到模块的根名称空间中。

Your two options are 您的两个选择是

import * as CONSTANTS from 'foobar'

or 要么

import {FOO, BAR} from 'foobar'

sources: 资料来源:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import

http://2ality.com/2014/09/es6-modules-final.html http://2ality.com/2014/09/es6-modules-final.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM