programing

Vuex 저장소 변환에 어레이 커밋

goodcopy 2022. 8. 24. 00:00
반응형

Vuex 저장소 변환에 어레이 커밋

파일 저장:

state: {
    ...
    cases: [],
    ...

mutations: {
    setCases(state, items) {
        // items contains only the first object in the array
    ... 

컴포넌트:

// resp is an array received from axios.get:

this.$store.commit({
     type: 'setCases',
     items: resp
})

response는 객체 배열입니다.Vuex가 setCases() 변환을 호출하면 배열의 첫 번째 개체만 'items'로 전달됩니다.

왜 그런 것일까요?

https://vuex.vuejs.org/guide/mutations.html (Object-Style Commit 섹션 참조)

오브젝트 스타일의 커밋을 사용하면 오브젝트 전체가 변환 핸들러에 페이로드로 전달되므로 핸들러는 그대로 유지됩니다.

resp에는 「」으로 할 수 있어야 합니다.items.items변이 함수에서.

언급URL : https://stackoverflow.com/questions/58107449/committing-array-to-vuex-store-mutation

반응형