路由补充

RangeError: Maximum call stack size

报以上错误。其大意是说栈内存溢出。
以下是我写的代码

1
2
3
4
5
router.beforeEach((to,from,next)=>{
if(!localStorage.getItem("token")){
next("/login")
}
})

现在分析一下。假设他跳转到login页面,然后他会判断是否有token,判断完成,他将眺望login页面。跳转之后他又会进行判断是否又token

因为他不是next(),按照他的流程接着跳转。而是采用了next(“/login”),表示他会重新发起一次跳转,也就是会再过一遍路由守卫。因此他会堆栈溢出而报错。
修改后的代码可以改为

1
2
3
4
5
6
7
8
9
router.beforeEach((to,from,next)=>{

if(to.path==="/login"){
next()
}
if(!localStorage.getItem("token")){
next("/login")
}
})