【问题标题】:Angular 2 route async resolve does not keep the location on navigateAngular 2 路由异步解析不会保留导航位置
【发布时间】: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


【解决方案1】:

问题在于 this.route 是在根级别注入的,因此它将成为根路由。

您可以遍历路线找到您感兴趣的路线。

【讨论】:

    【解决方案2】:

    我认为skipLocationChange 确实有效。问题是 Angular 没有导航到新路线,因为守卫失败了。如果要捕获失败的 URL,请检查 route: ActivatedRouteSnapshot 参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多