无法在rootpage ionic 2上调用provider方法

时间:2016-11-27 12:07:56

标签: angular ionic2

我有一个名为GlobalService的提供程序。 在GlobalService中,我创建了一个名为load的方法:

load() {
    if (this.data) {
      // already loaded data
      return Promise.resolve(this.data);
    }

    // don't have the data yet
    return new Promise(resolve => {
      // We're using Angular Http provider to request the data,
      // then on the response it'll map the JSON data to a parsed JS object.
      // Next we process the data and resolve the promise with the new data.
      this.http.get('https://randomuser.me/api/?results=10')
        .map(res => res.json())
        .subscribe(data => {
          // we've got back the raw data, now generate the core schedule data
          // and save the data for later reference
          this.data = data;
          resolve(this.data);
        });
    });
  }

我在事件" onPageDidEnter"上调用方法像这样。

this.globalService.load().then(data=>{
   console.log(data);
});

当我跑步时,有一条消息

  

typeError:无法读取属性' load'未定义的

这是我的rootpage的完整代码:

import { Component, Type, OnInit } from '@angular/core';
import { NavController, Page } from 'ionic-angular';
// import {InAppBrowser, InAppBrowserRef} from 'ionic-native';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

import { CariPesawatPage } from '../cari-pesawat/cari-pesawat';
import { CariHotelPage } from '../cari-hotel/cari-hotel';
import { CariKeretaPage } from '../cari-kereta/cari-kereta';
import { KonfirmasiPembayaranCreditCardPage } from '../konfirmasi-pembayaran-credit-card/konfirmasi-pembayaran-credit-card';
import { CariTourPage } from '../cari-tour/cari-tour';

import {GlobalService} from '../../providers/global-service/global-service';
import {PesawatService} from '../../providers/pesawat-service/pesawat-service';

@Component({
  templateUrl: 'build/pages/cari-tiket/cari-tiket.html',
  providers:[GlobalService, PesawatService]
})

export class CariTiket {
    static get parameters(){
        return [[NavController]];
  }

  onPageWillEnter() {
    console.log("Showing the first page!");
  }

    private pesawatPage: any;
    private hotelPage: any;
    private keretaPage: any;
    private tourPage: any;

    constructor(private navController: NavController, public globalService:GlobalService, public pesawatService:PesawatService, public http:Http) {
    // this.pesawatPage = KonfirmasiPembayaranCreditCardPage;
        this.pesawatPage = CariPesawatPage;
        this.hotelPage = CariHotelPage;
        this.keretaPage = CariKeretaPage;
        this.tourPage = CariTourPage;
        // this.pesawatService.loadAirlines().then(data => {
            // console.log(data);
        // });
    }

    onPageDidEnter (){
        this.globalService.load().then(data=>{
         console.log(data);
        });
    }
  openPage(page){
    //let browser: InAppBrowserRef = InAppBrowser.open('https://google.com', '_self', 'location=no');
    //browser.show();
    //browser.close();

    this.navController.push(page,null,{
      animate:true,
    });
  }
}
希望有人帮助我。

感谢。

0 个答案:

没有答案