CanActivate将我返回到空白页而不是重定向

时间:2018-05-20 15:54:45

标签: angular angular-guards

我的警卫让我回到空白页而不是重定向我。我的root也不是一个空白页面,它也应该发送给我登录。当我转到任何supervisor页面时会发生这种情况。

这些是我的路线:

export const appRoutes: Routes = [
  {path: '', component: LoginScreenComponent},
  {path: 'login', component: LoginScreenComponent},
  {path: 'student', component: StudentDownloadComponent, canActivate: [StudentGuard]},
  {path: 'student/download', component: StudentDownloadComponent, canActivate: [StudentGuard]},
  {path: 'student/upload', component: StudentUploadComponent, canActivate: [StudentGuard]},
  {path: 'supervisor', component: SupervisorUploadComponent, canActivate: [SupervisorGuard]},
  {path: 'supervisor/upload', component: SupervisorUploadComponent, canActivate: [SupervisorGuard]},
  {path: 'supervisor/logs', component: SupervisorLogsComponent, canActivate: [SupervisorGuard]},
  {path: 'supervisor/overzicht', component: SupervisorOverzichtComponent, canActivate: [SupervisorGuard]}
  ];

这是我的后卫:

@Injectable()
export class SupervisorGuard implements CanActivate {
  constructor(private supervisor: AuthService, private router: Router) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    if (this.supervisor.getSupervisorLoggedIn()) {
      return true;
    } else {
      this.router.navigate(['/login']);
      return false;
    }
  }
}

服务:

@Injectable()
export class AuthService {
  private isStudentLoggedIn;
  private isSupervisorLoggedIn;

  constructor() {
    this.isStudentLoggedIn = false;
    this.isSupervisorLoggedIn = false;
  }

  setStudentLoggedIn() {
    this.isStudentLoggedIn = true;
  }

  getStudentLoggedIn() {
    return this.isStudentLoggedIn();
  }

  setSupervisorLoggedIn() {
    this.isSupervisorLoggedIn = true;
  }

  getSupervisorLoggedIn() {
    return this.isSupervisorLoggedIn();
  }

}

1 个答案:

答案 0 :(得分:3)

在服务中,你试图调用isSupervisorLoggedIn就好像它是一个函数,但它只是一个布尔值。

您应该return this.isSupervisorLoggedIn;而不是