我正在Laravel 6 + Vue + Vuex创建一个聊天应用程序。我想调用 vuex store 并在调度操作完成后获取状态,然后我想在 vue 组件中对该状态进行一些处理。
在ChatWindow组件中
mounted: function () {
this.$store.dispatch('setContacts').then(() => {
console.log('dispatch called')
// I want to call the getter here and set one of the data property
});
}
Run Code Online (Sandbox Code Playgroud)
动作.js
setContacts: (context) => {
axios.post('/users').then(response => {
let users = response.data;
// consoled for testing
console.log(users);
context.commit('setContacts', users);
});
}
Run Code Online (Sandbox Code Playgroud)
mutators.js
setContacts: (state, users) => {
state.contacts = users;
},
Run Code Online (Sandbox Code Playgroud)
请参阅下面的屏幕截图。调度的 then 方法在action.js中的 …