2017-05-09 64 views
0

服务呼叫使用警卫,我尝试访问服务角2 - 内canActivate

,但我不能返回canActivate一个承诺(其中有我不能改变特定签名)

我autService返回一个承诺因为它是asynchrone

我如何能实现类似的东西:

@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;}); 

    } 
} 

感谢

+1

你知道canActivate可以返回'可观察 |服务承诺 | boolean'。 “我无法改变的签名”是什么意思? – echonax

+0

看看这个答案,我希望这有助于http://stackoverflow.com/questions/41463343/get-the-value-returned-by-canactivate-method-in-angular2-route-in-a-component/ 41467816#41467816 –

+0

好的,为什么他们不要在文档中谈论这个问题.... – ninja

回答

1

你只需要改变的签名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