It seems to be obvious, but I found myself a bit confused about when to use curly braces for importing a single module in ES6.(这似乎很明显,但是我发现自己对于何时使用花括号在ES6中导入单个模块感到有些困惑。)
For example, in the React-Native project I am working on, I have the following file and its content:(例如,在我正在从事的React-Native项目中,我具有以下文件及其内容:)
initialState.js
(initialState.js)
import initialState from './todoInitialState';
In the TodoReducer.js, I have to import it without curly braces:(在TodoReducer.js中,我必须不带花括号将其导入:)
import initialState from './todoInitialState';
If I enclose the initialState
in curly braces, I get the following error for the following line of code:(如果将initialState
用花括号括起来,则以下代码行将出现以下错误:)
Cannot read property todo of undefined(无法读取未定义的属性待办事项)
TodoReducer.js:
(TodoReducer.js:)
export default function todos(state = initialState.todo, action) { // ... }
Similar errors also happen to my components with the curly braces.(带有花括号的组件也发生类似的错误。) I was wondering when I should use curly braces for a single import, because obviously, when importing multiple component/modules, you have to enclose them in curly braces, which I know.(我想知道何时应该对大括号使用一次大括号,因为显然,当导入多个组件/模块时,必须将它们括在大括号中,这我知道。)
Edit:(编辑:)
The SO post at here does not answer my question, instead I am asking when I should or should not use curly braces for importing a single module, or I should never use curly braces for importing a single module in ES6 (this is apparently not the case, as I have seen single import with curly braces required)(此处的SO帖子没有回答我的问题,而是我在询问何时应该或不应该使用大括号来导入单个模块,或者我绝不应该在ES6中使用大括号来导入单个模块(这显然不是情况,如我所见,需要带花括号的单个导入))
ask by TonyGW translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…