반응형
Vuex를 사용하여 어레이의 개체를 업데이트하는 중
어레이 내의 개체를 Vuex로 업데이트하는 방법나도 해봤지만 소용이 없었어
const state = {
categories: []
};
// mutations:
[mutationType.UPDATE_CATEGORY] (state, id, category) {
const record = state.categories.find(element => element.id === id);
state.categories[record] = category;
}
// actions:
updateCategory({commit}, id, category) {
categoriesApi.updateCategory(id, category).then((response) => {
commit(mutationType.UPDATE_CATEGORY, id, response);
router.push({name: 'categories'});
})
}
[mutationType.UPDATE_CATEGORY] (state, id, category) {
state.categories = [
...state.categories.filter(element => element.id !== id),
category
]
}
이것은 일치하는 요소 없이 '카테고리' 배열을 원래 배열로 교체한 다음 업데이트된 요소를 배열 끝에 연결함으로써 작동한다.
이 방법에 대한 한 가지 주의할 점은 많은 경우 문제가 되지 않겠지만 배열 순서를 파괴한다는 것이다.그냥 명심해야 할 것.
참조URL: https://stackoverflow.com/questions/50422357/updating-object-in-array-with-vuex
반응형
'IT이야기' 카테고리의 다른 글
PYSONPATH에 디렉터리를 영구적으로 추가하시겠습니까? (0) | 2022.03.28 |
---|---|
Laravel/VueJS - 블레이드 내부의 여러 구성 요소가 제대로 작동하지 않음 (0) | 2022.03.28 |
반응 - useState의 setter 기능을 변경할 수 있는가? (0) | 2022.03.28 |
각도2 처리 http 응답 (0) | 2022.03.28 |
기능 구성 요소를 애니메이션으로 전달.애니메이션 구성 요소 생성 (0) | 2022.03.27 |