我正在尝试使用beforeEnter()
来保护全局router.js文件中的vue组件,但是在控制台上总是显示类型错误
无法读取未定义的属性获取器
这是router.js文件的示例
import login from './views/login'
//the component I want to guard
import admin from './views/adminPage'
//my Vuex store
import { store } from './store'
Vue.use(Router)
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/login',
name: 'login',
component: login
},
{
path: '/admin'
name: 'admin',
component: admin,
beforeEnter(to, from, next){
if(!store.getters.currentUser){
next('/login')
} else {
next()
}
}
}
]
})
//this is an example of my store.js file
export default new Vuex.Store({
state: {
currentUser: null
},
//....
getters: {
currentUser: state => state.currentUser
}
})
<
注意:我相信问题出在router.js文件中,因为我在所有应用程序文件中都调用了vuex吸气剂而没有问题。