Angular canActivate httpClient无限循环
我有这样的代码:
auth.guard.ts
canActivate(): void {
const token = localStorage.getItem('token');
const refreshToken = localStorage.getItem('refreshToken');
if (!token || !refreshToken) {
this.router.navigate('/login');
return false;
}
return this.authService.refresh().pipe(
map(() => { return true; }),
catchError(() => {
this.router.navigate('/login');
return of(false);
})
);
}
auth.service.ts
refresh(): Observable<any> {
return this.httpClient.post<any>(
environment.apiUrl+'/auth/token',
{
refreshToken: localStorage.getItem('refreshToken')
},
{ headers: this.headers }
);
}
但是“return this.authService.refresh().pipe(”行会无限循环。
如果我从 Angular HttpClient Observable 返回,可能会出现什么问题canActivate() 方法?
这种情况下无限循环的原因是什么?
I have code like this:
auth.guard.ts
canActivate(): void {
const token = localStorage.getItem('token');
const refreshToken = localStorage.getItem('refreshToken');
if (!token || !refreshToken) {
this.router.navigate('/login');
return false;
}
return this.authService.refresh().pipe(
map(() => { return true; }),
catchError(() => {
this.router.navigate('/login');
return of(false);
})
);
}
auth.service.ts
refresh(): Observable<any> {
return this.httpClient.post<any>(
environment.apiUrl+'/auth/token',
{
refreshToken: localStorage.getItem('refreshToken')
},
{ headers: this.headers }
);
}
But the row "return this.authService.refresh().pipe(" makes infinite loop.
What could be wrong if I return Angular HttpClient Observable from canActivate() method?
What is the reason of infinite loop in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
将 canActivate() 更改为:
Change canActivate() to this :