如何使用ES2015语法导入从文件导出的所有内容?有通配符吗? [英] How to import everything exported from a file with ES2015 syntax? Is there a wildcard?

查看:72
本文介绍了如何使用ES2015语法导入从文件导出的所有内容?有通配符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ES2015语法,我们有了新的导入语法,我一直在试图弄清楚如何将从一个文件导出的所有内容导入到另一个文件中,而不必将其包装在一个对象中,即。就好像它们是在同一个文件中定义一样。



所以,基本上,这个:

  // constants.js 

const MYAPP_BAR ='bar'
const MYAPP_FOO ='foo'





  // reducers.js 

import * from'./constants'

console.log(MYAPP_FOO)

这不起作用,至少根据我的Babel / Webpack设置,这种语法无效。



替代品



这样可行(但如果您需要输入的东西超过几件,则很长且烦人):

  // reducers.js 

从'./constants'导入{MYAPP_BAR,MYAPP_FOO}

console.log(MYAPP_FOO)

就像这样(但它包装对象中的consts):



< pre class =lang-js prettyprint-override> // reducers.js

import * as consts from'./constants'

console.log(consts.MYAPP_FOO)

是否有第一个变体的语法,或者你必须按名称导入每个东西,还是使用包装器对象?

解决方案


是否存在第一个变体的语法,


否。


或者你必须按名称导入每个东西,还是使用包装器对象?


是。


With ES2015 syntax, we have the new import syntax, and I've been trying to figure out how to import everything exported from one file into another, without having it wrapped in an object, ie. available as if they were defined in the same file.

So, essentially, this:

// constants.js

const MYAPP_BAR = 'bar'
const MYAPP_FOO = 'foo'

// reducers.js

import * from './constants'

console.log(MYAPP_FOO)

This does not work, at least according to my Babel/Webpack setup, this syntax is not valid.

Alternatives

This works (but is long and annoying if you need more than a couple of things imported):

// reducers.js

import { MYAPP_BAR, MYAPP_FOO } from './constants'

console.log(MYAPP_FOO)

As does this (but it wraps the consts in an object):

// reducers.js

import * as consts from './constants'

console.log(consts.MYAPP_FOO)

Is there a syntax for the first variant, or do you have to either import each thing by name, or use the wrapper object?

解决方案

Is there a syntax for the first variant,

No.

or do you have to either import each thing by name, or use the wrapper object?

Yes.

这篇关于如何使用ES2015语法导入从文件导出的所有内容?有通配符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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