目前我在屏幕上有多個下拉字段。當選擇的下拉值傳遞到查詢參數時,所以我想創(chuàng)建添加的動態(tài)查詢參數。我的代碼如下 // 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 中應用動態(tài)創(chuàng)建查詢參數的解決方案,但它不符合我的要求。那么你能幫助我嗎?
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 });
添加回答
舉報
0/150
提交
取消