如何订阅Ionic 2 platform.pause EventEmitter?

Zhi*_*ANG 10 ionic-framework ionic2 angular

我正在尝试使用以下代码进行订阅,但它不起作用.

import { Platform } from 'ionic-angular';
@Page({
    templateUrl: 'build/pages/test.html',
})    
export class Test{
    constructor(private platform: Platform) {
        this.platform.pause.subscribe(() => {
        console.log('paused')
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Ionic 2和TypeScript,Angular 2.正如platform.pauseIonic 2提供的EventEmitter,我想它应该可以订阅.但是,当我将应用程序放到后台时,console.log('pause')不会被解雇.

我应该添加Platform到提供商或类似的东西?另外,this.platform不是null.this.platform.ready().then(()=>{console.log('ready')})工作得很好.

Roq*_*que 10

我想你错过了platform.ready(),如下所示

constructor( private platform: Platform ) {
    platform.ready().then(() => {    
        this.platform.pause.subscribe(() => {
            console.log('[INFO] App paused');
        });

        this.platform.resume.subscribe(() => {
            console.log('[INFO] App resumed');
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

上面的代码对我有用.希望它也能帮到你.