gpt4 book ai didi

Angular 2 - canActivate 内的服务调用

转载 作者:行者123 更新时间:2023-12-03 15:07:10 26 4
gpt4 key购买 nike

使用守卫,我尝试访问服务

但我无法在 canActivate 中返回一个 promise (它具有我无法更改的特定签名)

我的 autService 返回一个 promise ,因为它是异步的

我怎样才能实现类似的目标:

@Injectable()
export class AuthGuardService implements CanActivate {
constructor(private authService: AuthService) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url;

this.authService.canAccessUrl(url)
.then( (answer:boolean) => {return answer;} );

}
}

谢谢

最佳答案

你只需要更改canActivate的签名即可

@Injectable()
export class AuthGuardService implements CanActivate {
constructor(private authService: AuthService) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
let url: string = state.url;

this.authService.canAccessUrl(url)
.then( (answer:boolean) => {return answer;} );

}
}

请查看https://angular.io/docs/ts/latest/api/router/index/CanActivate-interface.html

关于Angular 2 - canActivate 内的服务调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com