如何引用元素从状态foreach (Vuex)?



我在我的vue应用程序中使用vuex。In store是一个声明过的对象:

state: {
list: {
a: false,
b: false,
c: false
}
}

In mutations是在参数中接收数组的突变,例如:el: ['a', 'b']list对象处于状态时,el数组中的元素必须设置为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]。我没有得到一个错误,但它不工作。

使用括号符号[]动态获取属性:

mutations: {
SET_LIST(state, el) {
el.forEach(element => {
if (Object.keys(state.list).includes(element)) {
state.list[element] = true;
}
});
}
}

最新更新