在service ionic 3中调用方法时出错

时间:2017-12-31 12:05:21

标签: angular ionic-framework ionic2 ionic3

我有以下组件

newsfeed.ts

 import { Component, Inject } from '@angular/core';
 import { NavController, Tabs } from 'ionic-angular';
 import { AuthService } from '../../services/auth/auth.service';
 import { ReportService } from '../../services/reports/report.service';
 import { EnvVariables } from '../../app/environment-variables/environment-variables.token';

 @Component({
   selector: 'page-newsfeed',
   templateUrl: 'newsfeed.html'
 })

 export class Newsfeed {
   report_list: {};
   apiUrl: any;

   static get parameters()
   {
     return [[Tabs]];
   }

   constructor(
     public tab: Tabs,
     public navCtrl: NavController,
     public authService: AuthService,
     public reportService: ReportService,
     @Inject(EnvVariables) public envVariables) {

     this.tab = tab;
   }

   ionViewDidLoad() {
     this.reportList();
   }
   openReportTab() {
    this.tab.select(2);
   }

   reportList() {
     this.reportService.get_report_list().subscribe((results) => {
       this.report_list = results;
     })

   }

}

以及以下服务

report.service.ts

import { AuthHttp } from 'angular2-jwt';
import { Inject, Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from "rxjs/Observable";
import { EnvVariables } from '../../app/environment-variables/environment-variables.token';
import { GlobalErrorHandler } from '../providers/error-handler';
import { ToastHandler } from '../providers/toasts';

@Injectable()

export class ReportService {

    constructor(public authHttp: AuthHttp, private http: Http, @Inject(EnvVariables) public envVariables){

      var headers = new Headers();
      headers.append("Accept", 'application/json');
      headers.append('Content-Type', 'application/json');
      var options = new RequestOptions({headers: headers});
    }



    public get_report_list(): Observable<Response> {
        console.log("get report list")
        var url = "/api/v1/newsfeed";
        return this.authHttp.get(this.envVariables.apiEndpoint + url)
            .map(res => res.json().data)
    }

}

在我的代码中,我在newsfeed组件的构造函数中调用get_report_list方法,但我一直收到这个完全荒谬的错误

  Unhandled Promise rejection: Cannot read property 'get_report_list' of undefined ; Zone: 
             angular ; Task: Promise.then ; Value: [object Object] TypeError: Cannot read property 'get_report_list' of 
             undefined at Newsfeed.webpackJsonp.304.Newsfeed.reportList (http://localhost:8100/build/main.js:387:28) at 
             Newsfeed.webpackJsonp.304.Newsfeed.ionViewDidLoad (http://localhost:8100/build/main.js:362:14) at 
             ViewController._lifecycle (http://localhost:8100/build/vendor.js:18359:33) at ViewController._didLoad 
             (http://localhost:8100/build/vendor.js:18232:14) at Tab.NavControllerBase._didLoad 
             (http://localhost:8100/build/vendor.js:47611:18) at t.invoke 
             (http://localhost:8100/build/polyfills.js:3:8488) at Object.onInvoke 
             (http://localhost:8100/build/vendor.js:4796:37) at t.invoke 
             (http://localhost:8100/build/polyfills.js:3:8428) at r.run (http://localhost:8100/build/polyfills.js:3:3686) 
             at NgZone.run (http://localhost:8100/build/vendor.js:4665:62) 

完全混淆,我不知道为什么会出现这种错误。有人能指出我正确的方向吗?

由于

0 个答案:

没有答案