I am having trouble accessing data from the store
in the router
.
I have attempted three different methods, but none of them seem to be working correctly.
Here are the methods I tried:
// ReferenceError: store is not defined
console.log(store.state);
// ReferenceError: $store is not defined
console.log($store.state);
// TypeError: Cannot read property '$store' of undefined
console.log(this.$store.state);
Interestingly, when I try using it in App.vue
, it works fine.
This is how I used it in my App.vue file:
export default {
created() {
console.log(this.$store.state.user);
},
}
Can anyone provide some insight on what I might be doing wrong?
Here is a snippet of my store/index.js file:
state: {
user: {
loggedIn: false,
data: null
},
},
And here is a snippet from my router/inde.js file:
router.beforeEach((to, from, next)=>{
// console.log(store.state);
// console.log($store.state);
console.log(this.$store.state);
// if(this.$store.state.user.loggedIn){
// next('/login');
}else
next();
})