更新时间:2022-05-08 02:17:17
新HttpHeader
类的实例是不可变对象.调用类方法将返回一个新实例作为结果.因此,基本上,您需要执行以下操作:
The instances of the new HttpHeader
class are immutable objects. Invoking class methods will return a new instance as result. So basically, you need to do the following:
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');
或
const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
更新:添加多个标题
let headers = new HttpHeaders();
headers = headers.set('h1', 'v1').set('h2','v2');
或
const headers = new HttpHeaders({'h1':'v1','h2':'v2'});
更新:接受HttpClient标头的对象映射&参数
由于 5.0.0-beta.6 现在可以跳过创建HttpHeaders
对象直接将对象映射作为参数传递.因此,现在可以执行以下操作:
Since 5.0.0-beta.6 is now possible to skip the creation of a HttpHeaders
object an directly pass an object map as argument. So now its possible to do the following:
http.get('someurl',{
headers: {'header1':'value1','header2':'value2'}
});