【发布时间】:2017-05-03 18:17:44
【问题描述】:
在尝试访问不允许的页面时,我尝试将用户导航到错误页面。问题是 skipLocationChange 在这种情况下不起作用。它导航到错误页面,但 url 更改为根。如何保留用户提供的原始url?
resolve(route: ActivatedRouteSnapshot): Observable<any|boolean>|boolean {
return this.apiclientService.get('cars/' + route.params['id']).map(
response => {
if (response.data.user_id === this.authService.user().id) {
return response.data;
}
this.router.navigate(['/404'], { skipLocationChange: true });
return false;
}
).catch(error => {
this.router.navigate(['/404'], { skipLocationChange: true });
return Observable.of(false);
});
}
【问题讨论】:
-
你为什么不使用警卫? angular.io/docs/ts/latest/guide/router.html#!#guards 不确定这是否能解决您的问题,但我认为这正是警卫的用途。
-
我尝试了 guard (canActivate),但问题是一样的 + 无法像我们解决的那样将数据传递给控制器。如果您有任何示例,我将不胜感激。
-
抱歉,不知何故我认为您的代码在组件中。
resolve是我的意思。允许复制的 Plunker 也可以查看路线。 -
@izupet,你是怎么解决的?
标签: javascript angular typescript angular2-routing resolve