【发布时间】:2018-06-02 07:42:18
【问题描述】:
我对这些代码有问题,我创建了一个带有以下代码块的标题
headers.append("Authorization",btoa(username+":"+password));
var requestOptions = new RequestOptions({headers:headers});
但是如果我尝试在 post 方法中使用它
return this.http.post(url,JSON.stringify({username,password}),requestOptions)
.map(res=>res.json())
.map(res=>{
if(res){
localStorage.setItem("isLogged",res);
this.loggedIn =true;
}
return res;
});
我收到此错误消息
Typescript Error
Argument of type 'RequestOptions' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'. Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'. Index signature is missing in type 'Headers'.
我尝试将 Header() 更改为 HttpHeader() 但没有帮助。有什么问题?
更新
我删除了 requestOptions 对象,然后从 HttpHeaders() 创建了标头
let headers = new HttpHeaders();
并在 post 方法中使用此标头值
return this.http.post(url,JSON.stringify({username,password}), { options: { headers: headers; } })
.map(res=>res.json())
.map(res=>{
if(res){
localStorage.setItem("isLogged",res);
this.loggedIn =true;
}
return res;
});
然后得到这个错误
[ts]
Argument of type '{ options: { headers: HttpHeaders; }; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Object literal may only specify known properties, and 'options' does not exist in type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
我也试过了
return this.http.post(url,JSON.stringify({username,password}), { headers: headers })
.map(res=>res.json())
.map(res=>{
if(res){
localStorage.setItem("isLogged",res);
this.loggedIn =true;
}
return res;
});
然后我在第一个“.map”上遇到错误
[ts] Property 'map' does not exist on type 'Observable<Object>'.
【问题讨论】:
标签: angular typescript ionic-framework