- Import the store instance in the component where you want to dispatch the action:
import store from '@/store'; // Adjust the path based on your project structure
- Call the dispatch method on the store instance and pass the action name as the first argument, and any payload as the second argument (if needed):
store.dispatch('otherModule/actionName', payload);
- Ensure that the action is defined in the "otherModule" module in your Vuex store:
// In otherModule.js (or whatever your module file is named)
const actions = {
actionName({ commit }, payload) {
// Your action logic here
// You can commit mutations or perform async operations
}
};
export default {
actions,
// other module properties...
};
- (Optional) If you want to call an action from another module within a component method or lifecycle hook, you can use the mapActions helper:
import { mapActions } from 'vuex';
export default {
methods: {
...mapActions('otherModule', ['actionName']),
// Now you can call this.actionName() directly in your component
}
};