我该如何优化该代码,我的同事告诉我这段代码返回 true 和 false,我已经这样做了,但是他说它不正确并告诉我需要重新检查!
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this.token = localStorage.getItem('JWT_TOKEN');
if (!this.token) {
return this.router.navigate(['mypage']);
} else {
return true;
}
}
最佳答案
如果您更喜欢简短,这就是您所期望的吗?
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this.token = localStorage.getItem('JWT_TOKEN');
return !!this.token || this.router.navigate(['mypage']);
}
关于javascript - 如何优化 canActive Angular 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57107426/