admin管理员组文章数量:1200961
Given the following modules, how do I import the constants module and avoid having the default property included:
// constants.es6
export default {
foo: 'foo',
bar: 'bar'
}
// anotherModule.es6
import * as constants from './constants';
results in constants.default.foo
I can't seem to get the syntax correct to end up with constants.foo
Given the following modules, how do I import the constants module and avoid having the default property included:
// constants.es6
export default {
foo: 'foo',
bar: 'bar'
}
// anotherModule.es6
import * as constants from './constants';
results in constants.default.foo
I can't seem to get the syntax correct to end up with constants.foo
- stackoverflow.com/questions/43937561/… – João Otero Commented Aug 28, 2018 at 0:42
3 Answers
Reset to default 12import constants from './constants'
console.log(constants.foo, constants.bar)
If you would like to import the constants directly from ./constants
constants.js:
export const foo = 'foo'
export const bar = 'bar'
anotherModule.js:
import {foo, bar} from './constants'
console.log(foo,bar)
You shouldn't use an object for defining constants. Calling code is free to do constants.foo = 42;
and change the value.
Use
export const foo = 'foo';
export const bar = 'bar';
instead.
Then the import statement you have, import * as constants from './constants';
, will work as well.
If you don't want to change the way you define the constants, then your question is rather "how do I import a default export", which is answered in these questions:
- When should I use curly braces for ES6 import?
- What is "export default" in javascript?
export default {
foo: 'foo',
bar: 'bar'
}
// anotherModule.es6
import const from './constants';
Then
const.foo
本文标签: javascriptCorrect syntax to import constants in ES6Stack Overflow
版权声明:本文标题:javascript - Correct syntax to import constants in ES6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738547062a2096630.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论