我正在尝试检查用户已经选择了要登录的公司的天气。如果他们选择,那么他们会看到那些公司员工。否则,我们将重定向到登录页面。
我已经使用 Angular Route Guard 来做到这一点。但它没有继续这条路线并停在那里,尽管我返回真实。如何继续路线
路由配置
const appRoutes: Routes = [
{ path: "employees", component: EmployeeListComponent, canActivate: [AuthGuard] },
{ path: "", redirectTo: "/employees", pathMatch: "full" },
{ path: "login", component: LoginComponent },
{ path: "**", component: PageNotFoundComponent }
];
认证卫士
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AppService } from '../../app/app.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private appService: AppService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
let user: IUserViewData;
var _this = this;
this.appService.getUserViewData()
.subscribe(
(response: IUserViewData) => {
user = response;
if (user.LoggedInCompany != null) {
return true;
} else {
_this.router.navigate(['/login']);
return false;
}
},
(error: any) => { });
return false;
}
}