Angular2 - HTTP RequestOptions HEADERS

时间:2017-04-04 11:11:56

标签: angular http typescript tslint

我目前对tslint有一个问题,希望有人能指出我正确的方向。

我尝试使用Angular2框架提供的HTTP发送HTTP GET请求。有了这个请求,我必须指定内容类型和承载认证令牌。

我的代码示例:

let headers = new Headers();
let authToken = this._user.getUser().JWT;
headers.append('Content-Type', 'application/json');
headers.append('Authorization', `Bearer ${authToken}`);
let options = new RequestOptions({ headers: headers });

this._http.get('http://' + url '/', options)
            .timeout(3000)
            .subscribe(
                (res) => {

然而,这是有效的,tslint抱怨

  

" TS2345:类型的参数' {headers:Headers; }'不能转让给   类型' RequestOptionsArgs'的参数。财产类型'标题'   是不相容的。输入'标题'不能分配类型'标题'。   存在两种具有此名称的不同类型,但它们是不相关的。   物业'钥匙'类型' Headers''

中缺少

我感谢支持。

4 个答案:

答案 0 :(得分:199)

<强>更新

截至今天,@angular/httpdeprecated,而@angular/common/http应改为使用import { HttpHeaders } from '@angular/common/http';。因此,使用http标头的最佳方法是导入Headersdocumentation)。

旧答案

您要导入的import { Headers } from '@angular/http';类型为*NEWRECORD RECTYPE = D MH = Calcimycin AQ = AA AD AE AG AI AN BI BL CF CH CL CS CT EC HI IM IP ME PD PK PO RE SD ST TO TU UR ENTRY = A-23187|T109|T195|LAB|NRW|NLM (1991)|900308|abbcdef ENTRY = A23187|T109|T195|LAB|NRW|UNK (19XX)|741111|abbcdef ENTRY = Antibiotic A23187|T109|T195|NON|NRW|NLM (1991)|900308|abbcdef ENTRY = A 23187 ENTRY = A23187, Antibiotic MN = D03.633.100.221.173 PA = Anti-Bacterial Agents PA = Calcium Ionophores MH_TH = FDA SRS (2014) MH_TH = NLM (1975) ST = T109 ST = T195 N1 = 4-Benzoxazolecarboxylic acid, 5-(methylamino)-2-((3,9,11-trimethyl-8-(1-methyl-2-oxo-2-(1H-pyrrol-2-yl)ethyl)-1,7-dioxaspiro(5.5)undec-2-yl)methyl)-, (6S-(6alpha(2S*,3S*),8beta(R*),9beta,11alpha))- RN = 37H9VM9WZL RR = 52665-69-7 (Calcimycin) PI = Antibiotics (1973-1974) PI = Carboxylic Acids (1973-1974) MS = An ionophorous, polyether antibiotic from Streptomyces chartreusensis. It binds and transports CALCIUM and other divalent cations across membranes and uncouples oxidative phosphorylation while inhibiting ATPase of rat liver mitochondria. The substance is used mostly as a biochemical tool to study the role of divalent cations in various biological systems. OL = use CALCIMYCIN to search A 23187 1975-90 PM = 91; was A 23187 1975-90 (see under ANTIBIOTICS 1975-83) HN = 91(75); was A 23187 1975-90 (see under ANTIBIOTICS 1975-83) MR = 20160527 DA = 19741119 DC = 1 DX = 19840101 UI = D000001 *NEWRECORD RECTYPE = D MH = Temefos AQ = AA AD AE AG AI AN BL CF CH CL CS CT EC HI IM IP ME PD PK RE SD ST TO TU UR ENTRY = Abate|T109|T131|TRD|NRW|NLM (1996)|941114|abbcdef ENTRY = Difos|T109|T131|TRD|NRW|UNK (19XX)|861007|abbcdef ENTRY = Temephos|T109|T131|TRD|EQV|NLM (1996)|941201|abbcdef MN = D02.705.400.625.800 MN = D02.705.539.345.800 MN = D02.886.300.692.800 PA = Insecticides MH_TH = FDA SRS (2014) MH_TH = INN (19XX) MH_TH = USAN (1974) ST = T109 ST = T131 N1 = Phosphorothioic acid, O,O'-(thiodi-4,1-phenylene) O,O,O',O'-tetramethyl ester RN = ONP3ME32DL RR = 3383-96-8 (Temefos) AN = for use to kill or control insects, use no qualifiers on the insecticide or the insect; appropriate qualifiers may be used when other aspects of the insecticide are discussed such as the effect on a physiologic process or behavioral aspect of the insect; for poisoning, coordinate with ORGANOPHOSPHATE POISONING PI = Insecticides (1966-1971) MS = An organothiophosphate insecticide. PM = 96; was ABATE 1972-95 (see under INSECTICIDES, ORGANOTHIOPHOSPHATE 1972-90) HN = 96; was ABATE 1972-95 (see under INSECTICIDES, ORGANOTHIOPHOSPHATE 1972-90) MR = 20130708 DA = 19990101 DC = 1 DX = 19910101 UI = D000002

检查您的导入

答案 1 :(得分:8)

您必须按以下方式更新标头:

let headers =  {headers: new  HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded'})};

答案 2 :(得分:5)

Angular 5的更新

import { RequestOptions } from '@angular/http';

我在正确答案的评论中找到了这个,所以如果这有助于某人,祝你好运。

文档:https://angular.io/api/http/RequestOptions

答案 3 :(得分:1)

//内容类型Json的标头示例

import { Http, Headers, RequestOptions } from '@angular/http';

const Url = 'http://localhost:3000/';
const headers = new Headers;
const body = JSON.stringify({
title: "data" 
});
headers.append('Content-Type', 'application/json');
this.http.post(Url, body, { headers: headers })
.pipe(map((res => res)));