Sim*_*mon 10 angular2-routing angular
您好我遇到了问题.当我尝试导航到不被允许的页面时,我的CanActivate警卫会被调用两次,因为我没有登录.
我有1个根模块,并提供了我的CanActivate后卫和其他服务.
先感谢您!
这是我的路由器:
const appRoutes: Routes = [
{
path: "",
pathMatch: "full",
redirectTo: "/meal-list",
},
{
path: "login",
component: LoginComponent,
},
{
path: "meal-list",
component: MealListComponent,
canActivate: [AuthActivateGuard],
}
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, {useHash: true});
Run Code Online (Sandbox Code Playgroud)
守卫:
@Injectable()
export class AuthActivateGuard implements CanActivate {
constructor(private authService: AuthService,
private router: Router) {
console.log("guard created");
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|boolean {
if (!this.authService.authenticated) {
return this.authService.checkLogged().map(res => {
this.authService.authenticated = true;
return true;
}).catch(()=> {
this.authService.authenticated = false;
this.router.navigate(["login"]);
return Observable.of(false);
});
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这不是解决方案,但它是一个答案:
使用哈希路由时会发生这种情况(useHash:true)。
这可能是 Angular 路由器中的一个错误。
我仍在调查是否有解决方案。
史蒂夫
| 归档时间: |
|
| 查看次数: |
2979 次 |
| 最近记录: |