我很惊讶在这个主题上找不到任何线程。
在Ionic 2中,NavController中有页面的生命周期:ionView[didLoad|didLeave|...]
并且应该像这样调用Cordova events:
document.addEventListener("pause", onPause, false);
我处于想要获得科尔多瓦事件的情况。页面的离子生命周期不合适,因为当设备进入onResume
状态时,我想要做的事情需要发生。
我还没有尝试过,因为我希望在此之前找到一个良好的领导继续,但我觉得document
将无法访问来自Angular2,Ionic2,我可能需要添加一项服务来访问window
,就像解释here一样。
或者在Ionic 2中有没有其他已知方法可以访问document.addEventListener(...)
?
答案 0 :(得分:39)
它在Ionic 2中的工作方式详细here
基本上,您需要在页面中注入Platform
实例并订阅pause
事件发射器:
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();
}
}
答案 1 :(得分:4)
我还能够从Ionic2页面的构造方法中获取document.addEventListener,例如。
document.addEventListener("pause", function() {
// do something
}, true);
document.addEventListener("resume", function() {
// do something
}, true);