Ionic 2如何使用Cordova事件暂停恢复 [英] Ionic 2 How to use Cordova events pause resume

查看:210
本文介绍了Ionic 2如何使用Cordova事件暂停恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶没有找到任何关于该主题的线程。

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.

我还没有尝试过,因为我希望在此之前找到一个良好的领导继续前进,但我感觉,我可能需要添加一项服务来访问窗口,就像它被解释一样此处

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

基本上,你需要注入平台一世您的页面中的nstance并订阅暂停事件发射器:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆