【发布时间】:2021-11-01 13:48:07
【问题描述】:
我在我的 vue 应用程序中使用 vuex。在 store 中是一个声明的对象:
state: {
list: {
a: false,
b: false,
c: false
}
}
In 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]。然后我没有收到错误,但它不起作用。
【问题讨论】: