[英]Vuex My component doesn't react to state changes
I can't manage to make my component to react to Vuex store change, but only when I modify an existing element.我无法让我的组件对 Vuex 存储更改做出反应,但只有当我修改现有元素时。 From all I know it should not be related at all to how components are nested or something;
据我所知,它根本不应该与组件的嵌套方式或其他东西有关; After hours of debugging, trying everythig, all I have is something about deep watching the store changes...
经过数小时的调试,尝试了一切,我所拥有的只是深入观察商店的变化......
When I'm adding an element in currentVisit.pathologies ex: {id:1,title:"Something"} it works just fine, but when I edit an element my component doesn't react to store update当我在 currentVisit.pathologies ex: {id:1,title:"Something"} 中添加一个元素时,它工作得很好,但是当我编辑一个元素时,我的组件对存储更新没有反应
UPDATED
addPathology: (state, payload) => {
state.currentVisit.pathologies.push(payload);
},
updatePathology: (state, payload) => {
//this is the "reduced" code, this is how I found my component doesn't react.
state.currentVisit.pathologies[0] = payload;
}
state{
currentVisit: {
id: "5",
tumors: null,
pathologies: [],
examinations: null,
allergies: null,
diagnoses: null,
comorbidities: null,
chemotherapies: null,
radiations: null,
immunotherapies: null,
operations: null
}
}
createPathology: ({ commit, getters }, payload) => {
return new Promise((resolve, reject) => {
//do axios
//then commit updatePathologies
//then return promise response
baseAxios
.post("visits/" + getters.visitId + "/pathologies", payload)
.then(res => {
commit("addPathology", res.data.data);
resolve(res);
})
.catch(e => {
reject(e);
});
});
}
editPathology: ({ commit, getters }, payload) => {
return new Promise((resolve, reject) => {
baseAxios
.patch(
"visits/" + getters.visitId + "/pathologies/" + payload.id,
payload
)
.then(res => {
//commit("updatePathology", res.data.data);
resolve(res);
})
.catch(e => {
reject(e);
});
});
}
Change the updatePathology
mutation for reactivity:更改
updatePathology
突变的反应性:
updatePathology: (state, payload) => { const pathologies = state.currentVisit.pathologies; pathologies[0] = payload; // assign a new array reference for reactivity state.currentVisit.pathologies = [...pathologies]; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.