'Trying to dispatch action from Vuex module doesn't work. [vuex] unknown action type
I'm trying to register a Vuex module without success. Here's my code:
stores/index.js:
import Vuex from 'vuex';
import resourcesModule from './resources';
import axios from '@/helpers/axiosInterceptor';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
resources: resourcesModule
},
state: {...}})
stores/resources/index.js
export default {
state: {
resources: []
},
actions: {
fetchResources(store) {
const request = axios.get('/api/musical_resources');
request.then((res) => {
store.commit('setResources', res.data);;
});
return request;
}
},
mutations: {
setResources(state, payload) {
state.resources = payload;
}
},
getters: {
resources({resources}) {
return resources;
}
}
};
I'm trying to dispatch the fetchResources action from another component and that gives me " [vuex] unknown action type: fetchResources"
When I inspect the store object, in _modules property there is only the root property, so I suspect maybe I'm missing something when registering the module.
When I console.log(resourcesModules); in stores/index.js file everything looks fine.
So, what's my problem? Thank you very much
EDIT:
Also tryied this in stores/index.js:
import Vue from 'vue';
import Vuex from 'vuex';
import axios from '@/helpers/axiosInterceptor';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
resources: {
state() {
return {
resources: []
}
},
actions: {
fetchResources(store) {
const request = axios.get('/api/musical_resources');
request.then((res) => {
store.commit('setResources', res.data);;
});
return request;
}
},
mutations: {
setResources(state, payload) {
state.resources = payload;
}
},
getters: {
resources({ resources }) {
return resources;
}
}
}
},
state: {...}})
Without success
Solution 1:[1]
Try this way:
export default new Vuex.Store({
modules: {
resourcesModule
},
state: {...}})
to dispatch fetchResources
or
export default new Vuex.Store({
modules: {
resources: resourcesModule
},
state: {...}})
to dispatch resources.fetchResources
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nikola Pavicevic |
