gpt4 book ai didi

Angular2 - HTTP RequestOptions header

转载 作者:太空狗 更新时间:2023-10-29 16:45:39 27 4
gpt4 key购买 nike

我目前在使用 tslint 时遇到问题,希望有人能为我指出正确的方向。

我正在尝试使用 Angular2 框架提供的 HTTP 发送 HTTP GET 请求。对于此请求,我必须指定内容类型和不记名身份验证 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 提示

"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 已为 deprecated , 而应该使用 @angular/common/http 。因此,使用 http header 的最佳方法是导入 import { HttpHeaders } from '@angular/common/http'; ( documentation )。

旧答案

您应该导入的 Headers 类型是 import { Headers } from '@angular/http';

检查你的导入

关于Angular2 - HTTP RequestOptions header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com