获取当前打开的详情页面实例的 ID

Getting ID of detail-page instance that is currently open

如何处理 PushNotifications 在我的 Ionic3 / AngularJS 移动应用程序打开时向我的用户显示?我可以识别用户当前所在的页面,我可以通过以下方式做到这一点:

if ( (this.navCtrl.getActive().component === Page1) ){
    // user is currently on Page1
    // handle push notification
}

不幸的是,这还不够。 Page1是一个详情页,展示了一个item的详情。假设有 3 件商品:香蕉 (id: 1)、苹果 (id: 2) 和橙子 (id: 3)。

我需要知道,用户当前是在页面 1 中查看 item1、item2 还是 item3。我想做这样的事情:

if ( (this.navCtrl.getActive().component === Page1 && data.itemID === "page1-itemId") ){
    // data.itemID is the ID i get from the push notification
    // "page1-itemId" is what I desire to get somehow
}

我怎样才能知道 page1-itemId

你可以做一些事情,比如从导航到 Detail 组件的组件。

this.navCtrl.push(DetailPage, {
  id: "1",
});

然后在 Detail 页面中读取该 ID param,就像这样。不要忘记在构造函数

中导入和注入Navparams
import { NavParams } from 'ionic-angular';

constructor(private navParams: NavParams) {
  let id = navParams.get('id');
}

来源Ionic NavController docs

以防万一,任何人都会遇到同样的事情:我决定为此目的使用事件。

每当打开第 1 页时,我都会发布一个这样的事件:

Page1.ts

constructor(...
     public events: Events
) {
     // read NavParams
     this.itemId= this.navParams.get('itemId');

     // publish event with current itemId
     this.events.publish("Page1:opened",this.itemId);
}

然后我在我的 app.component.ts

中收听这个事件
// Listen to Opening of Confirm-Page
this.events.subscribe('Page1:opened', itemId=> {
   this.page1ItemId= itemId;
});

... 并结合使用 this.page1ItemIdthis.nav.getActive().component === Page1 以我想要的方式处理通知。