angular - Angular中canactivate方法中的API调用
问题描述
我在 Angular中canActivate
使用。guards
我想检查用户是否经过身份验证并根据结果保护路由。有两种类型的用户:Type1
和,Type2
因此用户可以使用 或 进行身份验证。以下内容供用户使用。Type1
Type2
unauthenticated
guard
Type1
这是我的代码:
constructor(private authservice: AuthService, private router: Router, private route: ActivatedRoute){}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean{
const self = this;
const expectedType = "type1";
this.authservice.isUserAuthenticatedbyType(expectedType).then(
function(data){
if(data === false){
console.log(data);
self.router.navigate(['/'], {relativeTo: self.route});
}
return data;
},
function(error){
self.router.navigate(['/']);
return false;
}
);
return false;
}
问题是我进行了 API 调用以验证用户是否已通过身份验证并return false;
在 API 结果之前执行。所以,暂时我看到一个不同的页面,然后它被路由到正确的页面。我该如何解决这个问题,我不想在 API 调用之前返回 false 或 true,但不这样做会产生错误。
我还尝试了以下方法:
return this.authservice.isUserAuthenticatedbyType(expectedType)
但这只是在用户http://localhost:4200
的情况下将我导航到 url unauthenticated
。
我有以下路线:
{ path: "", component: HomeComponent },
所以,在上面的场景中,HomeComponent
应该已经调用了,但是ngOnInit
HomeComponent 没有被调用。
解决方案
你可以像这样实现它:
角度 <= 7.0.0
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authservice.isUserAuthenticatedbyType("type1").pipe(
map(data => {
if (data === false) {
this.router.navigate(['/']);
return false;
}
return !!data;
}),
catchError(() => {
this.router.navigate(['/']);
return of(false);
}),
);
}
角度 >= 7.1.0
从 Angular 7.1.0 开始(注意它不在7.0.x 中),你也可以这样做,如果你有多个守卫,这会更短且更可预测:
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authservice.isUserAuthenticatedbyType("type1").pipe(
map(data => data === false ? this.router.parseUrl("/") : !!data)
catchError(() => this.router.parseUrl("/")),
);
}
推荐阅读
- c# - 当由 Azure 代理执行时,CopyFromScreen 方法永远不会完成
- html - 如何使 flexbox 响应式并将 1 堆叠在一起
- python - 需要帮助在 python 中为 UNO 纸牌游戏使用 continue 函数
- python - Python:解析包含标头的 XML (xliff) 文件
- c - 迭代器提供其当前元素的“视图”
- dart - Dart 实现一个类也会继承它的字段吗?
- ruby - 如何修复“无法构建 gem 原生扩展”。几个红宝石包?
- node.js - 有没有办法在 node.js 中加载和写入整个文件夹?
- python - 尽管 keras.json 指定了 tensorflow,但 Keras 使用 theano 后端
- vuejs2 - Vue 数据“计数器”不会在站点切换时迭代(但会在控制台中计数)