The error message `[vuex] unknown action type: getCompanies` indicates that Vuex cannot find the action `getCompanies` in the current namespace. Since you are using namespaced modules, the action should be dispatched with the module namespace prefix.
In your `HelloWorld` component, you are dispatching the action as:
this.$store.dispatch('company/getCompanies');
This is correct in terms of naming the action, but if you’re seeing an error, it usually means Vuex is not recognizing the action. Here are a few things to check:
1. Action Name: Ensure that `getCompanies` is correctly exported and named in your `actions.js` file.
2. Module Namespacing: Ensure that the `company` module is correctly namespaced in your `index.js` file of the module.
3. Store Setup: Verify that the module is correctly registered in your `store.js`.
Make sure that `getCompanies` is correctly exported. The code looks fine but ensure it’s properly saved and there are no typos.
Your `index.js` looks correct, but double-check it:
import actions from './action';
import getters from './getters';
import mutations from './mutations';
const state = {
companies: [],
};
export default {
namespaced: true,
state,
actions,
getters,
mutations,
};
Ensure that the module is correctly registered in `store.js`:
import Vue from 'vue';
import Vuex from 'vuex';
import companyModule from './modules/company';
Vue.use(Vuex);
const store = new Vuex.Store({
modules: {
company: companyModule,
},
});
export default store;
In your `HelloWorld` component, make sure you are mapping getters correctly and dispatching actions with the correct namespace.
import { mapGetters } from 'vuex';
export default {
computed: {
...mapGetters({
companies: 'company/allCompanies',
}),
},
mounted() {
this.$store.dispatch('company/getCompanies');
},
};
Ensure you are using a compatible version of Vuex with the Vue version you are using. Sometimes, compatibility issues can cause unexpected errors.
Work with our skilled Vue developers to accelerate your project and boost its performance.
Hire Vuejs Developers