【发布时间】:2020-01-15 12:07:37
【问题描述】:
我在 vuex 存储中有一个名为权限的变量。我希望我的组件在 getPermissions 更改时触发重新渲染。在 vue devtools 中,我清楚地看到商店中的状态发生了变化,但组件仍然从 getPermissions 获取旧状态。为了让我看到更改,我必须进行刷新。这和我变异它的方式有关吗?还是它是一个对象?
填充后看起来像这样:
permissions: {
KS1KD933KD: true,
KD9L22F732: false
}
我使用这种方法对其进行突变并使用 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);
}
};
在组件中我使用 mapGetters 来访问它
computed: {
...mapGetters('agentInfo',['getPermissions'])
}
为了更新权限值,我使用此操作(在更新值之前确实需要成功的 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}`
});
}
}
}
【问题讨论】:
-
.不应该是:..mapGetters(['agentInfo','getPermissions'])
-
不,我有不同的模块,在这种情况下,这意味着:从模块 agentInfo 获取 getter "getPermissions"