简体   繁体   English

对象上的 Vuex 状态更改不会触发重新渲染

[英]Vuex state change on object does not trigger rerender

I have a variable in the vuex store called permissions.我在 vuex 商店中有一个名为权限的变量。 And i want my component to trigger a rerender when the getPermissions changes.我希望我的组件在 getPermissions 更改时触发重新渲染。 In the vue devtools i clearly see that the state has changed in the store, but the component stil get the old state from getPermissions.在 vue devtools 中,我清楚地看到 store 中的状态已经改变,但组件仍然从 getPermissions 获得旧状态。 In order for me to see changes, I have to do a refresh.为了让我看到变化,我必须刷新。 Has it something to do with the way i mutate it?这与我变异它的方式有关吗? or the fact that it is an object?或者它是一个对象的事实?

It looks like this when populated:填充时看起来像这样:

permissions: { 
    KS1KD933KD: true,
    KD9L22F732: false
}

I use this method to do mutations on it and a getter to get it:我使用此方法对其进行更改并使用 getter 来获取它:

const getters = {
  getPermissions: state => state.permissions
};

const mutations = {
  set_recording_permissions(state, data) {
    let newList = state.permissions;
    newList[data.key] = data.bool;
    Vue.set(state, 'permissions', newList);
  }
};

And in the component i use mapGetters to get access to it在组件中,我使用 mapGetters 来访问它

computed: {
    ...mapGetters('agentInfo',['getPermissions'])
  }

In order to update the permissions value i use this action (it does require a succesfull api request before updating the value) :为了更新权限值,我使用此操作(在更新值之前确实需要成功的 api 请求):

const actions = {
  async setRecordingPermissions({ commit }, data) {
    let body = {
      agentId: data.userName,
      callId: data.callId,
      allowUseOfRecording: data.allowUseOfRecording
    };
    try {
      await AgentInfoAPI.editRecordingPermissions(body).then(() => {
        commit('set_recording_permissions', { key: data.callId, bool: data.allowUseOfRecording });
        commit('set_agent_info_message', {
          type: 'success',
          text: `Endret opptaksrettigheter`
        });
      });
    } catch (error) {
      console.log(error);
      commit('set_agent_info_message', {
        type: 'error',
        text: `Request to ${error.response.data.path} failed with ${error.response.status} ${error.response.data.message}`
      });
    }
  }
}

Since the getter only returns state variable you should use mapState, if you want to access it directly.由于 getter 只返回状态变量,如果您想直接访问它,您应该使用 mapState。

computed: mapState(['permissions'])

However, you can also use mapGetters, but then in your template, have to use getPermissions and not permissions .但是,您也可以使用 mapGetters,但是在您的模板中,必须使用getPermissions而不是permissions Example template:示例模板:

<ul id="permissions">
  <li v-for="permission in getPermissions">
    {{ permission }}
  </li>
</ul> 

If you have done this it is probably an issue with the object reference.如果您这样做了,则可能是对象引用的问题。 You use Vue.set , but you set the same object reference.您使用Vue.set ,但您设置了相同的对象引用。 You have to create a new object or set the key you want to update directly.您必须创建一个新对象或直接设置要更新的密钥。

new object新对象

let newList = { ...state.permissions };

Vue.set Vue.set

Vue.set(state.permission, data.key, data.value);

I don't know what the rest of you code looks like, but you will need to use actions to correctly mutate you store.我不知道你其余的代码是什么样的,但你需要使用操作来正确地改变你的存储。

For example:例如:

const actions = {
  setName({ commit }, name) {
    commit('setName', name);
  },
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM