如何在foreach(Vuex)中从状态中引用元素?
How to reference to element from state in foreach (Vuex)?
我在我的 vue 应用程序中使用 vuex。存储中是一个已声明的对象:
state: {
list: {
a: false,
b: false,
c: false
}
}
in mutations是在参数中接收数组的mutation,例如:el: ['a', 'b']
。
el
数组中的那些元素必须在 list
状态的对象中设置为 true。我正在为此使用 foreach 循环:
mutations: {
SET_LIST(state, el) {
el.forEach(element => {
if (state.list.element) {
state.list.element = true;
}
});
}
}
但我收到一个错误:error 'element' is defined but never used
。
因为element
没有用到不知道怎么正确引用
我在互联网上搜索并找到了这个解决方案:state.list[element]
。然后我没有收到错误,但它不起作用。
使用 the bracket notation []
动态获取 属性 :
mutations: {
SET_LIST(state, el) {
el.forEach(element => {
if (Object.keys(state.list).includes(element)) {
state.list[element] = true;
}
});
}
}
我在我的 vue 应用程序中使用 vuex。存储中是一个已声明的对象:
state: {
list: {
a: false,
b: false,
c: false
}
}
in mutations是在参数中接收数组的mutation,例如:el: ['a', 'b']
。
el
数组中的那些元素必须在 list
状态的对象中设置为 true。我正在为此使用 foreach 循环:
mutations: {
SET_LIST(state, el) {
el.forEach(element => {
if (state.list.element) {
state.list.element = true;
}
});
}
}
但我收到一个错误:error 'element' is defined but never used
。
因为element
没有用到不知道怎么正确引用
我在互联网上搜索并找到了这个解决方案:state.list[element]
。然后我没有收到错误,但它不起作用。
使用 the bracket notation []
动态获取 属性 :
mutations: {
SET_LIST(state, el) {
el.forEach(element => {
if (Object.keys(state.list).includes(element)) {
state.list[element] = true;
}
});
}
}