programing

Foreach(Vuex)의 상태로부터 요소를 참조하려면 어떻게 해야 합니까?

luckcodes 2022. 8. 2. 00:00

Foreach(Vuex)의 상태로부터 요소를 참조하려면 어떻게 해야 합니까?

vue 어플리케이션에서 vuex를 사용하고 있습니다.In store는 선언된 개체입니다.

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

돌연변이는 파라미터의 배열을 수신하는 돌연변이를 말합니다.다음은 예를 들어 다음과 같습니다.el: ['a', 'b']. 에 있는 요소들el에서 어레이를 true로 설정해야 합니다.list오브젝트가 스테이트에 있습니다.이 경우 포어치 루프를 사용합니다.

    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;
                }
            });
        }
    }

언급URL : https://stackoverflow.com/questions/69041192/how-to-reference-to-element-from-state-in-foreach-vuex