Vue JS 3 beforeEach in store
I can't get "store" information in "beforeEach" in route/index.ts file. It shows but gives "undefined"
Code image
Result "user" field is empty
Replies (2)
step 1 = "npm i vuex-persistedstate" and "npm i secure-ls" install
step 2 = store/index.ts in file paste
const store = createStore({
plugins: [
createPersistedState({
storage: {
getItem: key => ls.get(key),
setItem: (key, value) => ls.set(key, value),
removeItem: key => ls.remove(key)
}
})
],
modules: {
AuthModule,
BodyModule,
BreadcrumbsModule,
ConfigModule,
},
});
Hi,
You are setting the user object with the action VERIFY_AUTH, since it is asynchronous action it takes some time to set a user state.
store.dispatch(Actions.VERIFY_AUTH);
If you want to display the user object you can display it after some delay using currentUser getter.
setTimeout(() => {
console.log(store.getters.currentUser);
}, 1000);