在router文件夹下的index.js下写代码
- 在路由模块下写以下代码
// 路由守卫
router.beforeEach((to, from, next) => {
// if判断本地是否有token
if (localStorage.getItem('token')) {
next()//如果有token就直接进入下一个页面
} else {//没有token的话就判断
if (to.meta.requiredPath) {//判断该页面是否需要守卫
next('/Aaa')//如果需要就跳转到Aaa页面去登录
} else {//如果不需要就直接进入下一个页面
next()
}
}
})
- 在配置路由时开始配置需要拦截的页面
{
path: '/about',
meta: {
title: "about页面",
requiredPath:true
},
name: 'About',
component: () => import('../views/About.vue')
}
方法不止一种,目标只有一个