I have a index.js
file which contains my routes and I'm trying to create a beforeEnter
guard on the admin panel route and only let authenticated admins in. The problem is that when I console.log(store.getters.isLoggedIn)
, I get this:
? isLoggedIn(state) {
return state.user.token ? true : false
}
instead of true
or false
and I'm not sure why is this happening. My getter looks like this:
getters: {
isLoggedIn: state => {
return state.user.token ? true : false
}
}
and my routes are here:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import AdminPanel from '../views/AdminPanel.vue'
import store from "../store/authentication.js";
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home
},
{
path: "/admin",
component: AdminPanel,
beforeEnter: (to, from, next) => {
console.log(store.getters.isLoggedIn)
}
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
store.js
import Vue from 'vue'
import Vuex from 'vuex'
import authentication from './authentication'
import cart from './cart'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
authentication,
cart
}
})
export default store
authentication.js
const authentication = {
state: {
user: null
},
getters: {
isLoggedIn: state => {
return state.user.token ? true : false
}
}
}
export default authentication
question from:
https://stackoverflow.com/questions/65843993/how-to-access-a-vuex-getter-from-vue-router 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…