首页 > 代码库 > [Angular2 Router] Guard: CanLoad

[Angular2 Router] Guard: CanLoad

‘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.

[Angular2 Router] Guard: CanLoad