添加导入对象时的括号符号
Bracket notation while adding imported objects
在我的数组中的 redux 存储中,我存储了几个项目,我将它们作为导出注入到存储中。
为了可视化,我有单独的文件,我在其中存储如下内容:
export const item1 = {
name: 'item',
}
export const item2 = {
name: 'item',
}
等然后我像这样将其导入减速器:
import * as items from './items';
最后减速器看起来像这样:
stuff: [
items.item1,
items.item2
]
现在我想创建一个操作,允许我将更多对象添加到 reducer 数组中,这些对象将作为对象从文件中导入。试过括号符号,如:
{
...state[0],
stuff:[
...state[0].stuff,
items['action.item']
]
},
但是我收到错误 "export 'action.item' (imported as 'items') was not found in './items'" 所以我没主意了。
我该怎么做?操作将新项目名称作为字符串传递。
{
...state[0],
stuff:[
...state[0].stuff,
items[`${action.item}`]
]
},
据我了解,您将 action.item 作为字符串传递。试试上面的方法。它应该可以解决问题。
Note: if it's not intented mention full code for reducer file and also
items.js file
在我的数组中的 redux 存储中,我存储了几个项目,我将它们作为导出注入到存储中。
为了可视化,我有单独的文件,我在其中存储如下内容:
export const item1 = {
name: 'item',
}
export const item2 = {
name: 'item',
}
等然后我像这样将其导入减速器:
import * as items from './items';
最后减速器看起来像这样:
stuff: [
items.item1,
items.item2
]
现在我想创建一个操作,允许我将更多对象添加到 reducer 数组中,这些对象将作为对象从文件中导入。试过括号符号,如:
{
...state[0],
stuff:[
...state[0].stuff,
items['action.item']
]
},
但是我收到错误 "export 'action.item' (imported as 'items') was not found in './items'" 所以我没主意了。
我该怎么做?操作将新项目名称作为字符串传递。
{
...state[0],
stuff:[
...state[0].stuff,
items[`${action.item}`]
]
},
据我了解,您将 action.item 作为字符串传递。试试上面的方法。它应该可以解决问题。
Note: if it's not intented mention full code for reducer file and also items.js file