I've been refactoring my Vue SPA and one of the changes I was wanting to make has been the removal of manually importing "Store" in every component that needs it and instead uses the "this.$store.dispatch('module/update', data)", but it doesn't update the store like "Store.dispatch('module/update', data)" used to in the past.
methods: {
update() {
let office = {
id: this.office.id,
name: this.name,
description: this.description,
abbreviation: this.abbreviation
};
this.$http.post('/api/office/update', office).then(function(result) {
Store.dispatch('offices/update', result.data);
this.$router.push({ path: '/settings/offices' });
}).catch((error) => {
this.$router.push({ path: '/settings/offices' });
});
}
}
export const Offices = {
namespaced: true,
state: {
all: []
},
mutations: {
ADD_OFFICE: (state, offices) => {
offices.forEach(office => {
state.all.push(office);
});
},
DELETE_OFFICE: (state, id) => {
state.all.splice(state.all.map((office) => { return office.id }).indexOf(id), 1);
},
UPDATE_OFFICE: (state, data) => {
Object.assign(state.all.find((office) => office.id === data.id), data);
}
},
actions: {
get(context) {
Axios.get('/api/office/all').then((response) => {
context.commit('ADD_OFFICE', response.data);
});
},
create(context, office) {
context.commit('ADD_OFFICE', office);
},
update(context, data) {
context.commit('UPDATE_OFFICE', data);
},
delete(context, id) {
context.commit('DELETE_OFFICE', id);
}
}
}
I expected it to update the store as importing it manually does.