如何在angular 5中将header设置为HttpHeaders

如何在angular 5中将header设置为HttpHeaders,angular,xmlhttprequest,http-headers,angular5,Angular,Xmlhttprequest,Http Headers,Angular5,我试图弄清楚如何处理HttpHeaders上的头,以便通过HttpClient将它们用于http请求 const headers = new HttpHeaders(); headers.append('foo', 'bar'); headers.set('foo', 'bar'); console.log(headers.get('foo')) // null 它只能这样工作: const headers = new HttpHeaders().set('foo', 'bar'); con

我试图弄清楚如何处理HttpHeaders上的头,以便通过HttpClient将它们用于http请求

const headers = new HttpHeaders();
headers.append('foo', 'bar');
headers.set('foo', 'bar');

console.log(headers.get('foo')) // null
它只能这样工作:

const headers = new HttpHeaders().set('foo', 'bar');
console.log(headers.get('foo')) // bar
是否有添加标题的特殊方法?还是它是一个bug?

这对我很有用:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

const url = `https://sampleapi.com`;

@Injectable()
export class BasicService {
  private _headers = new HttpHeaders().set('Content-Type', 'application/json');
  constructor(private httpClient: HttpClient) { }

  getWithHeader(): Observable<any> {
    const headers = this._headers.append('foo', 'Bar');
    return this.httpClient.get<any>(url, { headers : headers });
  }
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从“rxjs/Observable”导入{Observable};
常量url=`https://sampleapi.com`;
@可注射()
导出类基本服务{
private _headers=new-HttpHeaders().set('Content-Type','application/json');
构造函数(私有httpClient:httpClient){}
getWithHeader():可观察的文档:不可变的Http头集,具有惰性解析。

这对我来说很有用:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

const url = `https://sampleapi.com`;

@Injectable()
export class BasicService {
  private _headers = new HttpHeaders().set('Content-Type', 'application/json');
  constructor(private httpClient: HttpClient) { }

  getWithHeader(): Observable<any> {
    const headers = this._headers.append('foo', 'Bar');
    return this.httpClient.get<any>(url, { headers : headers });
  }
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从“rxjs/Observable”导入{Observable};
常量url=`https://sampleapi.com`;
@可注射()
导出类基本服务{
private _headers=new-HttpHeaders().set('Content-Type','application/json');
构造函数(私有httpClient:httpClient){}

getWithHeader():Observable docs:不可变的Http头集,具有延迟解析。

我们可以使用headers.append-in-loop从json数组添加多个值吗?我们可以使用headers.append-in-loop从json数组添加多个值吗