vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航。有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的
判断用户有没有登录
// 全局路由守卫
router.beforeEach((to, from, next) => {
const userdata = Cookies.get('userdata')
if (!userdata) {
window.location.href = "login.html"
next(false)
return;
}
const userObj = JSON.parse(userdata)
if (userObj.uname !== 'admin') {
window.location.href = "login.html"
next(false)
return;
}
next()
})
本文详细介绍了如何使用vue-router的导航守卫功能进行用户登录验证,包括全局路由守卫的设置,确保未登录用户无法访问特定页面。

738

被折叠的 条评论
为什么被折叠?



