Angular2 - HTTP RequestOptions HEADERS [英] Angular2 - HTTP RequestOptions HEADERS
问题描述
我目前遇到tslint问题,希望有人能指出我正确的方向。
I currently have an issue with tslint and was hoping someone could point me in the right direction.
我正在尝试使用提供的HTTP发送HTTP GET请求通过Angular2框架。有了这个请求,我必须指定内容类型和不记名身份验证令牌。
I'm trying to send an HTTP GET request using HTTP provided by the Angular2 framework. With this request, I must specify the content-type and the bearer authentication token.
我的代码示例:
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抱怨
This works, however, tslint is complaining that
TS2345:'{headers:Headers;}'类型的参数不能分配给'RequestOptionsArgs'类型的
参数。属性'header'的类型
是不兼容的。类型'标题'不能分配给'标题'类型。
存在两个具有此名称的不同类型,但它们是不相关的。
类型'中缺少属性'keys' Headers'。
"TS2345: Argument of type '{ headers: Headers; }' is not assignable to parameter of type 'RequestOptionsArgs'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'Headers'. Two different types with this name exist, but they are unrelated. Property 'keys' is missing in type 'Headers'."
我很感谢支持。
推荐答案
更新
截至今天, @ angular / http
已已弃用, @ angular / common / http
应该用于ins因此,使用http标头的最佳方法是从'@ angular / common / http'导入 import {HttpHeaders};
(文档)。
As of today, @angular/http
has been deprecated, and @angular/common/http
should be used instead. So the best way to work with http headers is to import import { HttpHeaders } from '@angular/common/http';
(documentation).
旧答案
标题
你应该导入的类型是从{@ angular / http'导入{Headers};
。
The Headers
type you are supposed to import is import { Headers } from '@angular/http';
.
检查您的进口
这篇关于Angular2 - HTTP RequestOptions HEADERS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!