【问题标题】:how to added dynamic query params using string in Angular 7?如何在 Angular 7 中使用字符串添加动态查询参数?
【发布时间】:2019-10-03 12:38:35
【问题描述】:

目前我在屏幕上有多个下拉字段。 when selected dropdown value pass in the query param so I want to create dynamic query param added.我的代码在下面

     // this is one of the dropdown value
    if (this.SearchText) {
        query += 'q:' + SearchText + ',';
       }
// this is the second one of dropdown value 
    if (this.MakeId) {
        makename = this.SpaceToDash(this.MakesList.find(x => x.Id === +this.MakeId).Name);
        navigateUrl += '/' + makename;
        query += 'merk:' + this.MakeId + ',merkname:' + makename + ',';
       }
    this.router.navigate([ navigateUrl ], { queryParams: { query } });

因此,如果“MakeId”不是下拉值,则不应在“queryParams”中添加,我该怎么做。上面的解决方案不起作用。

Dynamically create query Params in Angular 2 应用解决方案,但它不符合我的要求。那你能帮我看看吗?

【问题讨论】:

  • 您必须在 queryParams 中传递键和属性的对象而不是字符串

标签: java angular


【解决方案1】:

QueryParams 应该采用 Object 而不是字符串,

这样就可以了

let query = {};
// this is one of the dropdown value
if (this.SearchText) {
    query['q'] =  SearchText;
   }
// this is the second one of dropdown value 
if (this.MakeId) {
    makename = this.SpaceToDash(this.MakesList.find(x => x.Id === +this.MakeId).Name);
    navigateUrl += '/' + makename;
    query['merk'] =  this.MakeId;
    query['makename'] =  makename;
   }
this.router.navigate([ navigateUrl ], { queryParams:  query  });

【讨论】:

  • 在生成 url 时,它被附加在 url "query=%5Bobject%20Object%5D" 后面
  • @UrvishPatel 确保您将正确的对象传递给 queryParams {queryParams: query } 而不是 {queryParams: {query} }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 2016-04-16
  • 1970-01-01
相关资源
最近更新 更多