繁体   English   中英

如何在 Angular2 中更新矩阵参数

[英]How to update matrix parameters in Angular2

在 Angular2 中,有没有办法更新矩阵参数,但导航到同一个组件? 基本上想从如下所示的 url 轻松转换:

/search;term=paris;pageSize=24;currentPage=1;someFilter=value;

同样的事情,但在分页时更新 currentPage :

/search;term=paris;pageSize=24;currentPage=2;someFilter=value;

使用router.navigate似乎不是最好的选择,因为我必须编写大量自己的逻辑来重新构建 url 以供navigate使用(重新构建已经存在的东西)。

对于踢腿和傻笑,我确实尝试过

this._router.navigate([this._router.url, {currentPage: this.pagination.currentPage}]);

但是(如您所料),路由器对当前 url 上有矩阵参数这一事实很愚蠢,因此结果并不漂亮。

编辑:还应该提到可能有任意数量的矩阵参数的附加键/值对,因此对任何路由进行硬编码都是不可能的

编辑:我从那以后尝试使用preserveQueryParams: true像:

this._router.navigate(["..", {currentPage: this.pagination.currentPage}], {preserveQueryParams: true});

为了获得易于使用/可转移的解决方案,但这并没有将矩阵参数保留在路线上。

更新:我已经实现了我自己的逻辑来获得所需的行为,所以这里是解决方案的代码片段:

  let currentParamsArr: Params = this.route.snapshot.params;
  let currentParamsObj: any = { };
  for (let param in currentParamsArr) {
    currentParamsObj[param] = currentParamsArr[param];
  }
  currentParamsObj.currentPage = +pageNum; //typecasting shortcut

  this._router.navigate([currentParamsObj], { relativeTo: this.route });

这段代码循环遍历参数(就像它们在快照中一样),并从中创建一个对象,然后添加/编辑我想要更新的参数,然后导航到“新”路由

但是,这并不漂亮,因为我将在程序的许多地方基本上具有相同的逻辑,或者必须对 Router 进行修改或提供一些其他通用方法。

最终对我来说效果很好的方法是这样的:

let route: ActivatedRoute;
const newUrl = router.createUrlTree([
  merge({'a': 123}, route.snapshot.params)
], {relativeTo: route});

通过使用merge,你可以添加、更新和减去url参数,然后使用router.navigateByUrl(newUrl)来执行。

add: merge({newParam: 111}, route.snapshot.params)

update: merge({param: 111}, route.snapshot.params)

subtract: merge({param: null}, route.snapshot.params)

希望其他人觉得这和我一样有用。

另一个使用 Object.assign 而不是 merge 的例子:

let route = this.route; // The imported ActivatedRoute
let currentParamsObj: Params = Object.assign({}, route.params);

let newParam = {};
newParam[key] = value;

let newUrl = this._router.createUrlTree([
        Object.assign(currentParamsObj, newParam)
    ], {relativeTo: this.route });

this._router.navigateByUrl(newUrl);

你有没有试过使用类似的东西

this._router.navigateByUrl('/search;term='+this.term+';pageSize='+this.pageSize+';currentPage='+this.currentPage+';someFilter='+this.someFilter;

就我而言,我有一个父子路由器,我需要修改父级上的参数: /my-parent;x=1;y=2/my-child

使用@Corbfon 的解决方案,我能够像这样修改父路由:

    // modify the context params in the PARENT route
    const newParam = {'z': '3'};
    const parentParams: Params = Object.assign({ ...this.activatedRoute.parent.snapshot.params }, newParam);

    // note: this approach doesn't preserve params on the child route (but there aren't any in my case)
    const currentChildPath = this.activatedRoute.routeConfig.path;

    // notice it's relativeTo the parent route
    const newUrl = this.router.createUrlTree([parentParams, currentChildPath],
      {relativeTo: this.activatedRoute.parent});
    this.router.navigateByUrl(newUrl);

希望这对其他人有帮助。

PS 不要忘记在ActivatedRoute上使用.snapshot.params而不是.params

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM