2016-11-05 47 views
5

我很惊讶没有找到任何线程已经在该主题上创建。离子2如何使用科尔多瓦事件暂停继续

离子2存在的页面中NavController生命周期:ionView[didLoad|didLeave|...]

而且还有一些应该在Cordova events被称为像这样: document.addEventListener("pause", onPause, false);

我在,我想情况获得科尔多瓦事件。页面的离子生命周期并不合适,因为当设备进入onResume状态时(无论显示哪个页面),我想要做的事情都需要发生。

我还没有尝试过,因为我希望能在这里找到一个不错的领先优势之前一直下去,但我有一种感觉,document将无法​​从Angular2,Ionic2访问的,我会可能需要添加一项服务才能访问window,正如here所解释的那样。

还是有其他已知的方式来访问document.addEventListener(...)时,在离子2?

回答

23

它工作在离子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(); 
    } 
} 
+0

我现在不会尝试,但这似乎是正确的和记录,所以我验证答案。 – nyluje

+0

暂停时可以做些什么?至于什么是暂停和恢复? – SR1

3

我也能从Ionic2页面的构造函数方法中获取document.addEventListener

document.addEventListener("pause", function() { 
    // do something 
    }, true); 

    document.addEventListener("resume", function() { 
    // do something 
    }, true);