文章内容

2019/5/5 15:52:57,作 者: 黄兵

this.router.navigate 无法获取参数相关问题

最近在使用Angular 4,点击跳转的写法如下:

this.router.navigate( this.router.createUrlTree(['account/verifyemail'],
      {queryParams: userId, skipLocationChange: true, preserveQueryParams: true }));

定义了一个查询参数UserIdskipLocationChange导航时不向history中添加记录,preserveParams 将查询参数带到下一个导航。

但是在verifyemail无法获取查询参数。

修改源代码:

 redirectVerifyEmail(userId): void {
    this.router.navigate(['/account/verifyemail'], {
      queryParams: {UserId: userId},
      queryParamsHandling: 'merge',
      skipLocationChange: true,
      preserveQueryParams: true
    });
  }

增加了queryParams: 'merge',合并已有的 queryParams 到当前的 queryParams 中。

verifyemail,获取参数,具体代码如下:

this.sub = this.activatedRoute.queryParams.subscribe((params: Params) => {
      const key = 'UserId';
      console.log(params);
      this.UserId = params[key];
      console.log(this.UserId);
      // get email address.

    });


参考资料:

1、NavigationExtras

2、RouterLink 指令简介

3、Angular Router: Query Parameters


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - this.router.navigate 无法获取参数相关问题

分享到:

发表评论

评论列表