为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Angular 7 中使用字符串添加动态查询参数?

如何在 Angular 7 中使用字符串添加动态查询参数?

翻过高山走不出你 2023-09-20 17:35:58
目前我在屏幕上有多个下拉字段。当选择的下拉值传递到查询参数时,所以我想创建添加的动态查询参数。我的代码如下     // 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”中,那么我该怎么做。上述解决方案不起作用。在 Angular 2 中应用动态创建查询参数的解决方案,但它不符合我的要求。那么你能帮助我吗?
查看完整描述

1 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

QueryParams 应该采用一个对象而不是字符串,


所以你可以用这种方式来做


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  });


查看完整回答
反对 回复 2023-09-20
  • 1 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号