CanActivate vs. CanActivateChild with component-less routes

后端 未结 6 1162
傲寒
傲寒 2020-12-12 18:17

The angular2 documentation about Route Guards left me unclear about when it is appropriate to use a CanActivate guards vs. a CanActivateChild guard

6条回答
  •  清歌不尽
    2020-12-12 18:42

    I also confused the angular2's documentation about routeGuard. what's the difference between the CanActivate guard and CanActivateChild guard.

    I have some findings,I hope this will help you.

    in the auth-guard.service.ts file

      canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let url: string = state.url;
    
    return this.checkLogin(url);
    }
    
    canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    return this.canActivate(route, state);
    }
    

    because the canActivate method is called in the canActivateChild function. you can write a snippet of code that don't call the canActivate method in the canActivateChild function.

提交回复
热议问题