使用追加或设置的设置标题在Angular 2中不起作用 [英] Set Headers using append or set does not work in Angular 2

查看:69
本文介绍了使用追加或设置的设置标题在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.appendheaders.set,但是都抛出了

I have tried headers.append and headers.set but both throws

Cannot read property 'append' of null
Cannot read property 'set' of null

推荐答案

您忘记了吗?

let headers: Headers = new Headers();

这篇关于使用追加或设置的设置标题在Angular 2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆