How to get Vuex mutation access in a js file outside of vue components. #8814
-
Hello, my vuex store: export default function (/* { ssrContext } */) {
const Store = new Vuex.Store({
modules: {
// example
},
state: {
myThing: '',
},
mutations: {
setMyThing(state, newThing) {
state.myThing = newThing;
},
},
// enable strict mode (adds overhead!)
// for dev mode only
strict: process.env.DEBUGGING,
});
return Store;
} My functions.js: import store from '../../../../src/store/index';
export function setTheNewThing(newData) {
store.commit('setMyThing', newData);
} This is where I call it in a component: import '../functions/functions';
...
created() {
setTheNewThing('set');
this.newValue = this.$store.state.myThing;
}, When I call setTheNewThing in a vue component (mounted), I could sort of hack it by passing $this.store into my setTheNewThing (which might be OK for testing actually), or I could just return the data and set it to Vuex out here in the created() where it has valid context, but it just feels like there should be a way to get the Vuex store into non-vue a function javascript file like this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
use injection: export function setTheNewThing(store, newData) {
store.commit('setMyThing', newData);
} |
Beta Was this translation helpful? Give feedback.
use injection: