How to dispatch action from a javascript file (instead of a vue component)? How to dispatch action from a javascript file (instead of a vue component)? vue.js vue.js

How to dispatch action from a javascript file (instead of a vue component)?


First, create the store in store.js.

import Vue from 'vue';import Vuex from 'vuex';import actions from './actions';import mutations from './mutations';import getters from './getters';const store = new Vuex.Store({    modules: {},    state: {      location: null    },    actions,    mutations,    getters,  });export default store

Then, import the store into jsfile.js and use it.

import store from "./store"const detail = {};detail.validateLocation = (location) => {  // Use imported store  store.dispatch('SET_LOCATION', {city: 'California'})}export default detail;

Assuming you have a main or index file that creates your Vue instance, you now likely need to change the import from importing the create function to simply importing the store.