Accessing Vuex Store in Boot files
-
I want to use vuex in a boot file to read user’s saved access token and info from LocalStorage and commit the data to store and use it in other boot files (for example axios boot file) and vue components. I tried to export Vuex instance from the src/store/index but I get the following error:
Uncaught ReferenceError: Cannot access 'store' before initialization
Here is my src/store/index:
import Vue from "vue"; import Vuex from "vuex"; import auth from "./auth"; let store = null; Vue.use(Vuex); export default function (/* { ssrContext } */) { const Store = new Vuex.Store({ modules: { auth, }, }); store = Store; return Store; } export { store };
and I just imported the instance in boot files:
import { store } from "src/store";
By the way I tried putting a
console.log()
in the createStore function in store/index and it doesn’t show up. -
@cphm The boot file default function gets passed the store instance:
export default async ({ app, router, store, Vue }) => { // use store here, no need to import it }