这似乎很明显,但是我发现自己对于何时使用花括号在ES6中导入单个模块感到有些困惑。例如,在我正在从事的React-Native项目中,我具有以下文件及其内容:
initialState.jsvar initialState = {
todo: {
todos: [
{id: 1, task: 'Finish Coding', completed: false},
{id: 2, task: 'Do Laundry', completed: false},
{id: 2, task: 'Shopping Groceries', completed: false},
]
}
};
export default initialState;
在TodoReducer.js中,我必须不带花括号将其导入:
import initialState from './todoInitialState';
如果将initialState
花括号括起来,则以下代码行将出现以下错误:
TodoReducer.js:无法读取未定义的属性待办事项
export default function todos(state = initialState.todo, action) {
// ...
}
带有花括号的组件也发生类似的错误。我想知道何时应该为单个导入使用花括号,因为显然,当导入多个组件/模块时,必须将它们括在花括号中,这我知道。
编辑:
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)
花括号在命名导出时仅用于导入。如果默认为导出,则不使用花括号导入。