本文介绍了Angular 4.3-HttpClient设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
let httpParams = new HttpParams().set('aaa', '111');
httpParams.set('bbb', '222');
为什么这不起作用?它只会设置'aaa'而不是'bbb'
Why this doesn't work?It only set the 'aaa' and NOT the 'bbb'
我还有一个对象{aaa:111,bbb:222}如何设置所有值而不循环?
Also, I have an object { aaa: 111, bbb: 222 }How can I set all the values without looping?
UPDATE(这似乎有效,但是如何避免循环?)
UPDATE (this seems to work, but how can avoid the loop?)
let httpParams = new HttpParams();
Object.keys(data).forEach(function (key) {
httpParams = httpParams.append(key, data[key]);
});
推荐答案
5.0.0-beta.6之前
let httpParams = new HttpParams();
Object.keys(data).forEach(function (key) {
httpParams = httpParams.append(key, data[key]);
});
从5.0.0-beta.6起
可以直接传递对象而不是HttpParams.
Going forward the object can be passed directly instead of HttpParams.
getCountries(data: any) {
// We don't need any more these lines
// let httpParams = new HttpParams();
// Object.keys(data).forEach(function (key) {
// httpParams = httpParams.append(key, data[key]);
// });
return this.httpClient.get("/api/countries", {params: data})
}
这篇关于Angular 4.3-HttpClient设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!