CanActivate

一个接口,某些类可以实现它以扮演一个守卫,来决定该路由能否激活。 如果所有守卫都返回 true,就会继续导航。如果任何一个守卫返回了 false,就会取消导航。 如果任何一个守卫返回了 UrlTree,就会取消当前导航,并开始导航到这个守卫所返回的 UrlTree

Interface that a class can implement to be a guard deciding if a route can be activated. If all guards return true, navigation will continue. If any guard returns false, navigation will be cancelled. If any guard returns a UrlTree, current navigation will be cancelled and a new navigation will be kicked off to the UrlTree returned from the guard.

查看"说明"...

      
      interface CanActivate {
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
}
    

说明

class UserToken {} class Permissions { canActivate(user: UserToken, id: string): boolean { return true; } } @Injectable() class CanActivateTeam implements CanActivate { constructor(private permissions: Permissions, private currentUser: UserToken) {} canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree { return this.permissions.canActivate(this.currentUser, route.params.id); } } @NgModule({ imports: [ RouterModule.forRoot([ { path: 'team/:id', component: TeamCmp, canActivate: [CanActivateTeam] } ]) ], providers: [CanActivateTeam, UserToken, Permissions] }) class AppModule {}
      
      
  1. class UserToken {}
  2. class Permissions {
  3. canActivate(user: UserToken, id: string): boolean {
  4. return true;
  5. }
  6. }
  7.  
  8. @Injectable()
  9. class CanActivateTeam implements CanActivate {
  10. constructor(private permissions: Permissions, private currentUser: UserToken) {}
  11.  
  12. canActivate(
  13. route: ActivatedRouteSnapshot,
  14. state: RouterStateSnapshot
  15. ): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
  16. return this.permissions.canActivate(this.currentUser, route.params.id);
  17. }
  18. }
  19.  
  20. @NgModule({
  21. imports: [
  22. RouterModule.forRoot([
  23. {
  24. path: 'team/:id',
  25. component: TeamCmp,
  26. canActivate: [CanActivateTeam]
  27. }
  28. ])
  29. ],
  30. providers: [CanActivateTeam, UserToken, Permissions]
  31. })
  32. class AppModule {}

你还可以转而实现一个带有 canActivate 签名的函数:

You can alternatively provide a function with the canActivate signature:

@NgModule({ imports: [ RouterModule.forRoot([ { path: 'team/:id', component: TeamCmp, canActivate: ['canActivateTeam'] } ]) ], providers: [ { provide: 'canActivateTeam', useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true } ] }) class AppModule {}
      
      
  1. @NgModule({
  2. imports: [
  3. RouterModule.forRoot([
  4. {
  5. path: 'team/:id',
  6. component: TeamCmp,
  7. canActivate: ['canActivateTeam']
  8. }
  9. ])
  10. ],
  11. providers: [
  12. {
  13. provide: 'canActivateTeam',
  14. useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
  15. }
  16. ]
  17. })
  18. class AppModule {}

方法

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
      
      canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
    
参数
route ActivatedRouteSnapshot
state RouterStateSnapshot
返回值

Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree