在Ionic v3中调用提供程序函数时,无法读取未定义的属性“ example”

时间:2019-02-20 16:48:19

标签: ionic-framework ionic3 provider

当我尝试在页面的构造函数中调用提供程序函数或使用ionViewWillEnter/ionViewDidLoad时遇到一个大问题。 在我看来,问题在于:调用函数时未定义提供程序。

import { SpotifyProvider } from './../../providers/spotify/spotify';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

/**
 * Generated class for the PlayerPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-player',
  templateUrl: 'player.html',
})
export class PlayerPage {

  seconds:any;
  minutes:any;

  constructor(public navCtrl: NavController, public navParams: NavParams, public spotify: SpotifyProvider) {
    (setInterval(this.timeConverter, 333));
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PlayerPage');
  }

  timeConverter() {
    var seconds = (this.spotify.currentSong.progress_ms)/100;
    var minutes = seconds/60;
    seconds = seconds-(minutes*60);
  }

}

在此示例中,指定的错误代码为:

运行时错误

Cannot read property 'currentSong' of undefined. currentSong is a variable
of SpotifyProvider which cannot be undefined (because it gets initialized 
before the problematic "PlayerPage" is opened/constructed).

有人知道这个问题的解决方案吗?

0 个答案:

没有答案