Angular 2 - Routing - CanActivate work with Observable

前端 未结 7 1052
终归单人心
终归单人心 2020-11-30 18:30

I have an AuthGuard (used for routing) that implements CanActivate.

canActivate() {
    return this.loginService.isLoggedIn         


        
7条回答
  •  野性不改
    2020-11-30 18:44

    You should upgrade "@angular/router" to the latest . e.g."3.0.0-alpha.8"

    modify AuthGuard.ts

    @Injectable()
    export class AuthGuard implements CanActivate {
        constructor(private loginService:LoginService, private router:Router) { }
    
        canActivate(next:ActivatedRouteSnapshot, state:RouterStateSnapshot) {
            return this.loginService.isLoggedIn().map(e => {
                if (e) {
                    return true;
                }
            }).catch(() => {
                this.router.navigate(['/login']);
                return Observable.of(false);
            });
        }   
    }
    

    If you have any questions, ask me!

提交回复
热议问题