首页 > 解决方案 > 离子应用程序正在编译并在构建时出现错误

问题描述

嗨,我是 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:  });

标签: angularionic-frameworkdjango-rest-frameworkionic3

解决方案


您应该像这样附加标头,使用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;
 });

推荐阅读