使用 append 或 set 设置标题在 Angular 2 中不起作用 [英] Set Headers using append or set does not work in Angular 2
本文介绍了使用 append 或 set 设置标题在 Angular 2 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是 Angular 2.4.8.我想为每个请求附加自定义标头.我正在使用 BaseRequestOptions
来定义自定义标头并将其添加到应用程序提供程序.以下是代码.
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);
}
}
提供者如下.
providers: [
{ provide: BaseRequestOptions, useClass: AppBaseRequestOptions }
]
我尝试过 headers.append
和 headers.set
但都抛出了
I have tried headers.append
and headers.set
but both throws
无法读取 null 的属性 'append'
无法读取 null 的属性 'set'
推荐答案
你忘记了吗?
let headers: Headers = new Headers();
这篇关于使用 append 或 set 设置标题在 Angular 2 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文