zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[Angular2 Router] Guard: CanLoad

Router Guard Angular2
2023-09-14 09:00:52 时间

'canLoad' guard can decide whether a lazy load module can be loaded or not.

 

@Injectable()
export class CanLoadPokemon implements CanLoad {

  constructor(private authService: AuthService) {

  }
  canLoad(route: Route): Observable<boolean>|Promise<boolean>|boolean {
    return this.authService.isAuth;
  }

}

 

app.routers.ts:

{path: 'home', loadChildren: 'app/home/home.module', data: {title: 'Pokemon List'}, canLoad: [CanLoadPokemon]},

 

So if user not login, app won't load home module.