更新时间:2022-04-25 02:26: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'}
});