我的守卫将我返回到一个空白页面,而不是重定向我。此外,我的 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();
}
}