Angular Route Guard CanActivate 停止路由
Angular Route Guard CanActivate stops routing
我正在尝试检查用户是否已经select登录了一家公司。如果他们 select 那么他们会看到那些公司员工。否则,我们将重定向到登录页面。
我已经使用 Angular Route Guard 来做到这一点。但它没有继续这条路线并停在那里,虽然我 return 是的。如何继续路线
路由配置
const appRoutes: Routes = [
{ path: "employees", component: EmployeeListComponent, canActivate: [AuthGuard] },
{ path: "", redirectTo: "/employees", pathMatch: "full" },
{ path: "login", component: LoginComponent },
{ path: "**", component: PageNotFoundComponent }
];
Auth Guard
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;
}
}
更改订阅地图 returns Observable<boolean>
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
var _this = this;
return this.appService.getUserViewData().map(user => {
if (user.LoggedInCompany != null) {
return true;
} else {
_this.router.navigate(['/login']);
return false;
}
});
}
我正在尝试检查用户是否已经select登录了一家公司。如果他们 select 那么他们会看到那些公司员工。否则,我们将重定向到登录页面。
我已经使用 Angular Route Guard 来做到这一点。但它没有继续这条路线并停在那里,虽然我 return 是的。如何继续路线
路由配置
const appRoutes: Routes = [
{ path: "employees", component: EmployeeListComponent, canActivate: [AuthGuard] },
{ path: "", redirectTo: "/employees", pathMatch: "full" },
{ path: "login", component: LoginComponent },
{ path: "**", component: PageNotFoundComponent }
];
Auth Guard
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;
}
}
更改订阅地图 returns Observable<boolean>
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
var _this = this;
return this.appService.getUserViewData().map(user => {
if (user.LoggedInCompany != null) {
return true;
} else {
_this.router.navigate(['/login']);
return false;
}
});
}