반응형
Vuex getter가 저장소 변환으로 새로 고쳐지지 않음
타이프 스크립트로 작성된 VueJs 프로젝트를 상속받았습니다.일부 데이터를 표시하는 컴포넌트가 있습니다.변환(ADD_SHOOL)을 통해 vuex 스토어가 업데이트되지만 변경 내용이 보기에 반영되지 않습니다.감시자가 필요한 것 같습니다만, Vue가 제 가게의 변화에 대응하려면 어떻게 해야 할지 모르겠습니다.
보다
data() {
return {
school: {} as ISchool
};
},
async mounted() {
await this.getSchoolInformation();
},
methods: {
async getSchoolInformation() {
this.school = await Stores.schoolStore.getSchool(1);
}}}
Store.ts
Vue.use(Vuex);
const modules: ModuleTree<IRootState> = {
schoolStore: schoolModule
};
const store: Store<IRootState> = new Store<IRootState>({
modules
});
디폴트 스토어 내보내기
SchoolStore.ts
export default class SchoolStore {
public async getSchool(id: number, isFirstLoad: boolean): Promise<ISchool> {
return Store.getters[SchoolNamespace + GetterTypes.GET_SCHOOL_FROM_STORE];
}
}
School Module.t
export const state: ISchoolState = {
schools: []
};
export const getters: GetterTree<ISchoolState, IRootState> = {
[GetterTypes.GET_SCHOOL_FROM_STORE]: state => {
const storeSchool: ISchool | undefined = state.schools.find(x => x.id === 1);
return storeSchool as ISchool;
}
};
export const mutations: MutationTree<ISchoolState> = {
[MutationTypes.ADD_SCHOOL](state: ISchoolState, school: ISchool): void {
const index: number = state.schools.findIndex(x => x.id === school.id);
if(index === -1) {
state.schools.push(school);
} else {
state.schools.splice(index, 1, school);
}}};
const schoolModule: Module<ISchoolState, IRootState> = {
actions,
getters,
mutations,
namespaced: true,
state
};
export default schoolModule;
인덱스.ts
const schoolStore: SchoolStore = new SchoolStore();
export default {
schoolStore
};
Ackroydd덕분에 저는 View에서 단순히 계산 속성을 사용하여 작업을 할 수 있게 되었습니다.또한 getSchool을 동기화할 수 있었습니다.
갱신된 표시
computed: {
school() {
return Stores.schoolStore.getSchool(1, false);
}
}
});
발생하고 있는 이 문제에 대해서는, 메뉴얼을 참조해 주세요.반응성 상세
변환 시 어레이 내의 항목을 업데이트하거나 어레이에 새 항목을 추가합니다.Vue는 거스름돈을 받지 않을 것이다.그러기 위해서는, 유저에게Vue.set
:
import Vue from 'vue'
...
if(index === -1) {
Vue.set(state.schools, state.schools.length, school);
} else {
Vue.set(state.schools, index, school);
}}};
언급URL : https://stackoverflow.com/questions/64446193/vuex-getter-not-refreshing-with-store-mutation
반응형
'programing' 카테고리의 다른 글
C 컴파일러와 C++ 컴파일러는 강제하지 않는데 함수 시그니처에 어레이 길이를 허용하는 이유는 무엇입니까? (0) | 2022.06.18 |
---|---|
오브젝트 프로포트에 필요한 오브젝트 속성이 있는지 확인하는 방법 (0) | 2022.06.18 |
Django 템플릿에 VueJs 컴포넌트 살포 (0) | 2022.06.17 |
선택 옵션에서 값을 비활성화하는 방법(vue.js 2) (0) | 2022.06.17 |
Vue.js를 사용하여 메타 제목 및 설명 변경 (0) | 2022.06.17 |