将需要验证的路由设置meta属性
在main.js页面设置路由验证
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.auth)){ // 判断该路由是否需要登录权限
if (localStorage.getItem("Authorization")) { // 判断当前的token是否存在
next();
}
else {
next({
path: '/login',
query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由
})
}
}
else {
next();
}
});