Angular 2 Guards:canLoad()任何原因两次射击?

时间:2017-08-18 15:22:58

标签: angular angular2-guards angular-router-guards angular-route-guards

canLoad在拒绝或甚至接受时会两次出现

export const ROUTES: Routes = [
    {
        path: 'Inbox',
        canLoad: [CanActivateViaAuthGuardService],
        loadChildren: 'LazyLoaded module',
    }
]

canLoad Implementation

canLoad(route: Route): Observable<boolean> {
    console.log("CanLoad Is Called?");

    return this._apsm.IsModuleCanbeLoaded()
      .map(
      res => {
        console.log(res);

        if (res === false) {
          // User:Don't Has Access
          this.router.navigateByUrl('/error');

          return false;
        }

        // User Has Access:
        return true;
      })
      .catch((error: any) => {
        console.log(error);
        this.router.navigateByUrl('/error');
        return Observable.of(false);
      });    
  }

调用Servie方法

假设将返回Observale布尔响应

IsModuleCanbeLoaded(): Observable<boolean> {         
    return false as Observable<boolean>;
}

0 个答案:

没有答案