直接上中间件代码
export default ({ store, route, redirect }) => {
const token = store.state.token
// 不需要登录能访问的的页面
const publicPages = [
'/area',
'/home',
'/home/edu_customize',
'/home/edu_staging',
'/home/free_price',
'/home/free_study',
'/home/whole_overseer',
'/big_forum',
'/big_forum/details',
'/educational_classroom',
'/organization_login',
'/organization_register',
'/school_lists',
'/school_lists/local_consultation',
'/school_lists/details',
'/school_lists/contrast',
'/security_services',
'/security_services/contrast',
'/student_login',
'/student_register',
'/study_release',
'/training_agency',
'/training_agency/service_instructions',
'/training_agency/details',
]
const authRequired = !publicPages.includes(route.path) // 访问其他页面需要登录才能访问
// 如果用户未登录,则重定向到登录页
if (!token && authRequired) {
return redirect('/home')
}
}
因为在有的项目里面,需要做路由排除,也就是没有token也能访问到的页面,就这样写了,有更好的方法可以在评论区留言