Ionic 2 如何使用 Cordova 事件暂停恢复 [英] Ionic 2 How to use Cordova events pause resume
问题描述
我很惊讶没有找到关于该主题的任何主题.
I am surprised to not find any thread already created on that topic.
在 Ionic 2 中,NavController 中有页面的生命周期:ionView[didLoad|didLeave|...]
In Ionic 2 there is the lifecycle of the pages in NavController: ionView[didLoad|didLeave|...]
还有 Cordova 事件应该这样称呼:document.addEventListener("pause", onPause, false);
我处于想要获得 Cordova 事件的情况.页面的离子生命周期不合适,因为我想做的事情需要在设备进入 onResume
状态时发生,无论页面显示在哪个页面上.
I am in a situation where I want to get the Cordova events. Ionic lifecylce of pages is not a fit because what I want to do need to happen when the device gets in the onResume
status whichever page shows on.
我还没有尝试过,因为我之前希望在这里找到一个好的线索继续前进,但我感觉 document
不会'不能从 Angular2、Ionic2 访问,我可能必须添加一个服务来访问 window
就像它解释的那样 这里.
I haven't tried it yet, because I was hoping to find a good lead here before to keep on going, but I have the feeling that document
won't be accessible from Angular2, Ionic2 and that I will probably will have to add a service to access the window
like it is explained here.
或者在 Ionic 2 中是否有任何其他已知方法可以访问 document.addEventListener(...)
?
Or is there anyother known way to access document.addEventListener(...)
when in Ionic 2?
推荐答案
详细介绍它在 Ionic 2 中的工作方式 这里
The way it works in Ionic 2 is detailed here
基本上,您需要在页面中注入 Platform
实例并订阅 pause
事件发射器:
Basically, you need to inject the Platform
instance in your page and subscribe to the pause
event emitter:
import { Component } from '@angular/core';
import { Subscription } from 'rxjs';
import { Platform } from 'ionic-angular';
@Component({...})
export class AppPage {
private onResumeSubscription: Subscription;
constructor(platform: Platform) {
this.onResumeSubscription = platform.resume.subscribe(() => {
// do something meaningful when the app is put in the foreground
});
}
ngOnDestroy() {
// always unsubscribe your subscriptions to prevent leaks
this.onResumeSubscription.unsubscribe();
}
}
这篇关于Ionic 2 如何使用 Cordova 事件暂停恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!