问题
I am using Angular 2.4.8. I want to append custom headers to each request. I am using BaseRequestOptions to define custom headers and adding it to app providers. Following is the code.
import { BaseRequestOptions, Headers } from '@angular/http';
import { Injectable } from '@angular/core';
@Injectable()
export class AppBaseRequestOptions extends BaseRequestOptions {
public merge(headers: Headers) {
headers.append('Content-type', 'application/json');
headers.append('My-Custom-Header', 'My-Custom-Header-Value');
return super.merge(headers);
}
}
Provider is as follows.
providers: [
{ provide: BaseRequestOptions, useClass: AppBaseRequestOptions }
]
I have tried headers.append and headers.set but both throws
Cannot read property 'append' of null Cannot read property 'set' of null
回答1:
Did you forget?
let headers: Headers = new Headers();
来源:https://stackoverflow.com/questions/42555482/set-headers-using-append-or-set-does-not-work-in-angular-2