【问题标题】:Runtime error: Tournaments is not defined运行时错误:锦标赛未定义
【发布时间】:2018-01-12 02:02:09
【问题描述】:

该应用程序是使用 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);
      }

    }

elite-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()));
            });

        }

    }

【问题讨论】:

    标签: typescript ionic-framework ionic2 ionic3 typescript2.0


    【解决方案1】:

    错误是因为 整个 url 应该在 `` 之间(现在 /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()));
            });
    
        }
    

    【讨论】:

    • 我问了另一个。为什么 ionViewDidUnload 在 Ionic 3 中不起作用?
    • ionViewDidLoad ionViewDidUnload? @PerinbanParameshwaran
    • ionViewDidUnload 顺便说一句不存在。 Here 你可以找到完整的生命周期事件列表@PerinbanParameshwaran
    • 检查此link 在此图像中检查控制台。它将被使用。也许我认为在这个版本中他们删除了它。那么如何卸载我们加载的View呢?
    • 好的,我试试...谢谢兄弟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2013-07-12
    • 1970-01-01
    • 2017-09-01
    • 2017-05-21
    • 2017-02-04
    相关资源
    最近更新 更多