全局守卫中使用localStorage.getItem('isLogin')报错
来源:9-2 路由守卫实现基础登陆校验功能
ForCoke
2022-01-29
router.beforeEach((to,from,next) => {
const isLogin = localStorage.getItem(‘isLogin’)
(isLogin || to.name === ‘Login’) ? next() : next({name: ‘Login’})
})
报错:TypeError: localStorage.getItem(…) is not a function
但是在守卫回调中,输出localStorage,其原型上是有getItem方法的。同时如果使用window. localStorage.getItem()也报错,请问一下老师是为什么?
老师我发现了,好像是没有加分号的原因,但是我在路由独享守卫中书写const isLogin = localStorage.getItem(‘isLogin’)的时候,也没加分号,但是此处就不报错。
写回答
1回答
-
Dell
2022-02-07
现在好用了?
012022-02-09
相似问题