简体   繁体   English

Vuex 我的组件对状态变化没有反应

[英]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]; }

Vue Docs Vue 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 状态更改后,React不会更新组件 - React doesn't update component after state changes 状态更改时,React组件不会重新呈现 - React component doesn't re-render when state changes 用户更改输入时,React不会更新组件状态 - React doesn't update component state when user changes input React 组件不会使用相同的道具重新渲染,但子状态会发生变化 - React component doesn't rerender with the same props but child state changes 反应中的setState函数不会更改组件的状态 - The setState function in react doesn't change the state of my component 在 state 更新后 React useEffect 不会重新加载我的组件 - React useEffect doesn't reload my component after state update 状态更改但组件不更新 - 角度 - State changes but component doesn't update - ANGULAR vuex状态和吸气剂已更新,但计算值不起作用 - vuex state and getters are updated, but computed value doesn't react 反应 - state 不会在 class 组件中更新,即使屏幕上打印的消息发生了变化 - React - state doesn't update in class component even though the message printed on the screen changes 当上下文状态改变时,React 钩子上下文组件不会重新渲染 - React hooks context component doesn't re-render when context state changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM