当我尝试在页面的构造函数中调用提供程序函数或使用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).
有人知道这个问题的解决方案吗?