angular - 离子应用程序正在编译并在构建时出现错误
问题描述
嗨,我是 ionic 新手,我使用 ionic 构建了一个应用程序,它在我的 PC 上编译时运行,当我尝试使用 ionic cordova run android 运行应用程序时,它显示错误,我发现所有变量我正在使用错误,我也无法构建应用程序。所以请任何人帮助我解决这个问题提前谢谢q这是我得到的错误
[12:00:54] typescript: src/pages/find-genie4/find-genie4.ts, line: 31
Argument of type '{ headers: { 'Authorization': string; }; }' is not assignable to parameter of type
'RequestOptionsArgs'. Types of property 'headers' are incompatible. Type '{ 'Authorization': string; }' is
not assignable to type 'Headers'. Object literal may only specify known properties, and ''Authorization''
does not exist in type 'Headers'.
L30: const token = JSON.parse(localStorage.getItem("userData"));
L31: this.http.get('https://api.findgenie.org/v1/my-requests/',{headers: {'Authorization': 'Token '+token.token}}
L32: this.posts = data.json().data.user_requests;
[12:00:54] typescript: src/pages/find-genie4/find-genie4.ts, line: 32
Property 'posts' does not exist on type 'FindGenie4Page'.
L31: this.http.get('https://api.findgenie.org/v1/my-requests/',{headers: {'Authorization': 'Token '+token.token}}).subscribe(data => {
L32: this.posts = data.json().data.user_requests;
L33: this.rating = data.json().data.user_details.rating;
[12:00:54] typescript: src/pages/find-genie4/find-genie4.ts, line: 33
Property 'rating' does not exist on type 'FindGenie4Page'.
L32: this.posts = data.json().data.user_requests;
L33: this.rating = data.json().data.user_details.rating;
L34: console.log("this.posts",this.posts)
[12:00:54] typescript: src/pages/find-genie4/find-genie4.ts, line: 34
Property 'posts' does not exist on type 'FindGenie4Page'.
L33: this.rating = data.json().data.user_details.rating;
L34: console.log("this.posts",this.posts)
L35: });
解决方案
您应该像这样附加标头,使用HttpHeaders
类发送标头。在您的响应数据中,将其类型声明为any
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Token '+token.token
})
};
this.http.get('https://api.findgenie.org/v1/my-requests/',httpOptions ).subscribe((data: any) => {
this.posts = data.json().data.user_requests;
this.rating = data.json().data.user_details.rating;
});
推荐阅读
- netlogo - 来自 NetLogo 补丁的记者
- c++ - 如何在程序崩溃时关闭程序,没有任何警告屏幕等... C++ Windows
- javascript - 单击图标时如何保存选定的日期选择器值?
- angular - 在所有挂起的订阅发出值之前,不会在路由导航上触发 ngOnDestroy
- vb.net - 用两个变量编辑 20 个列表框
- system.reactive - 寻找响应式方法来实现昂贵操作的触发器
- python - How to replace ™ in a string?
- node.js - 如何将控制器的返回数据用作 EJS 数据?
- json - Jolt generates null values as transformation output and can't be removed
- java - Android Firebase how to loop through EVERY key of the list