该应用程序是使用Ionic 3开发的。我无法从Firebase检索数据。当我尝试用函数调用Api时,它显示错误,表明它没有定义..
有什么方法可以解决这张照片。此外,ionViewDidUnload在Ionic 3中不起作用。我也尝试使用事件导入,但我还没有得到它。
Tournaments.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { TeamsPage } from '../teams/teams';
import { EliteApi } from '../../shared/elite-api'
@IonicPage()
@Component({
selector: 'page-tournaments',
templateUrl: 'tournaments.html',
})
export class TournamentsPage {
tournaments : any;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
private eliteApi : EliteApi) {
}
ionViewDidLoad(){
this.eliteApi.getTournaments().then(data => this.tournaments = data );
}
itemTapped($event, tourney){
this.navCtrl.push(TeamsPage, tourney);
}
}
精英api.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
@Injectable()
export class EliteApi{
private baseUrl = 'https://elite-schedule-c1e63.firebaseio.com//';
constructor(private http: Http){}
getTournaments(){
return new Promise(resolve => {
this.http.get('${this.baseUrl}'/tournaments.json)
.subscribe((res: Response) => resolve(res.json()));
});
}
}
答案 0 :(得分:1)
错误是因为整个网址应该在`之间(现在/tournaments.json
部分不是)。因此,在elite-api.ts
文件中,更改getTournaments()
方法,如下所示:
getTournaments(){
return new Promise(resolve => {
this.http.get(`${this.baseUrl}/tournaments.json`) // <- Like this :)
.subscribe((res: Response) => resolve(res.json()));
});
}