[Vuejs]-Vue : Accessing store prop/variable in route guard (route beforeEach)?

2๐Ÿ‘

โœ…

You can access the store. There is no problem with that as you are importing it.

The problem is with how you are accessing state.loggedInUser. You might be using modules in your store and loggedInUser might belong to a module. So to access it you need to refer to the module name that loggedInUser belongs to like this:

if (store.state.moduleName.loggedInUser) {
  ? console.log("You are logged in :) Go to requested page ") 
    next() 
}

Replace moduleName with the name of the module loggedInUser belongs to.

๐Ÿ‘คVamsi Krishna

0๐Ÿ‘

I was able to achieve this in a recent app by changing store.state.loggedInUser for store.getters.loggedInUser.

Essentially telling it to call the getter method directly instead of a mapping to the method through the state.

๐Ÿ‘คMartin Calvert

Leave a comment