我对这些代码有困难,我创建了一个包含以下代码块的标头
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,{ 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,{ headers: headers }) .map(res=>res.json()) .map(res=>{ if(res){ localStorage.setItem("isLogged",res); this.loggedIn =true; } return res; });
[ts] Property 'map' does not exist on type 'Observable<Object>'.