路由跳转$router
来源:9-2 路由守卫实现基础登陆校验功能
RadicalW
2021-04-26
做路由跳转时,this.$router.push('/')为什么不行
setup(){
const router = useRouter();
const handleLogin = () =>{
localStorage.isLogin = true;
router.push({name: 'Home'});
// 为什么$router不行
// this.$router.push('/')
}
return { handleLogin }
}
写回答
1回答
-
你在箭头函数里用了this,请注意,不要在箭头函数里用this。而且在setup函数里,也是不能用this的。直接写router.push({name: 'Home'})好了。
setup函数中,没有this,你可以看前面章节对setup的讲解。(因为setup创建时,当前对象this还未创建)
122021-04-26
相似问题