//路由拦截(导航守卫:前置导航守卫和后置导航守卫)
//前置守卫:
//三个参数:to 代表即将进入的路由 from 代表即将离开的路由 next() 每一个导航守卫至少搭配一个next()
router.beforeEach((to,from,next)=>{
//想要进购物车页面,必须有token
console.log('to',to);
console.log('from',from)
//获取token
let token=localStorage.getItem('token')
if(to.path=='/cart'){
//此时必须要有token
if(token){
next() //这个next()是争对购物车的
}else{
alert('请先登录')
}
return
}
next() //这个next()是适配所有的路由
})
路由的拦截(导航守卫)
最新推荐文章于 2025-04-02 22:56:43 发布
本文深入探讨了前端路由的拦截机制,即导航守卫。详细介绍了如何使用Vagrant配置开发环境,并结合Echarts展示动态数据加载过程中的路由控制策略,确保页面跳转的安全性和用户体验。
&spm=1001.2101.3001.5002&articleId=121369411&d=1&t=3&u=0ad7100288264b5caa5dc5895e002315)
430

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



