Angular 2 AuthGuard Service with redirect?

前端 未结 5 1163
南旧
南旧 2020-12-05 22:46

I have an application that I am building that implements CanActivate on the dashboard route. It works fine accept on page reload, I check a flag in the user service to see i

5条回答
  •  遥遥无期
    2020-12-05 23:22

    in AuthGuard Do this

    @Injectable()
    
    export class AuthGuard implements CanActivate {
    
    auth: any = {};
    
    constructor(private authService: AuthService, private router: Router) {
    
    }
    
    canActivate() {
        if (/*user is logged in*/) {
            this.router.navigate(['/dashboard']);
            return true;
        }
        else {
            this.router.navigate(['/Login']);
        }
        return false;
    }
    }
    

提交回复
热议问题