【问题标题】:How to know that ionic app going to closed or killed in ionic 3?如何知道 ionic 应用程序将在 ionic 3 中关闭或终止?
【发布时间】:2019-12-03 19:08:54
【问题描述】:

有什么方法可以知道我们的 ionic 应用程序正在关闭或被杀死?

【问题讨论】:

    标签: ionic2 ionic3


    【解决方案1】:

    您可以订阅Ionic platform servicepause 事件。文档这样描述暂停事件:

    本机平台放置应用程序时会发出暂停事件 进入后台,通常当用户切换到不同的 应用。

    David M. 就如何实现这个here 给出了一个很好的答案:

    import { Component } from '@angular/core';
    import { Subscription } from 'rxjs';
    import { Platform } from 'ionic-angular';
    
    @Component({...})
    export class AppPage {
      private onPauseSubscription: Subscription;
    
      constructor(platform: Platform) {
        this.onPauseSubscription = platform.pause.subscribe(() => {
           // do something when the app is put in the background
        }); 
      } 
    
      ngOnDestroy() {
        // always unsubscribe your subscriptions to prevent leaks
        this.onPauseSubscription.unsubscribe();
      }
    }
    

    首先,您必须将Platform 服务注入您的页面/组件,然后您才能订阅pause 事件。我只是稍微编辑了 Davids 代码示例,所以请给他一些功劳。


    Ionics Platform 服务只是 Cordovas 事件的包装器。因此,如果您更感兴趣,请查看 eventspause event 上的 Cordova 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 2019-09-14
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 2020-10-13
      • 2020-04-29
      相关资源
      最近更新 更多