首页 > 解决方案 > 每次通过路由器导航页面时,Ionic Platform ready 都会触发

问题描述

我在goPremium页面的构造函数中有以下代码

constructor(
    public alertCtrl: AlertController,
    private iap2: InAppPurchase2) {
   
    s.iap2.verbosity = this.iap2.DEBUG;
    this.platform.ready().then(() => {
      console.log('❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎Platform Ready❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎❤︎')
      this.setupProducts(); // Should be called ONLY ONCE. 
      this.iap2.refresh();
    });

  }

setupProducts()函数只需要调用一次,因为它从应用商店设置应用内产品。如果页面通过触摸底部选项卡导航,代码就可以正常工作。

但是,如果我使用来自另一个页面的路由,平台就绪会再次触发。

this.router.navigate(['tabs/gopremium', {accType: type}]);

因此,重复的事件被注册,这导致函数被多次执行。

根据文档,平台就绪应该只触发一次。

我也试过了ionViewDidLoad

ionViewDidLoad() {
    this.platform.ready().then(() => { 
          this.setupProducts(); // Should be called ONLY ONCE. 
          this.iap2.refresh();
    });
  }

如果我使用它,setupProducts()它甚至不会触发一次。

this.setupProducts()即使页面被浏览,如何只触发一次this.router.navigate

标签: angularionic-frameworkangular-routingangular-router

解决方案


我找到了答案,

而不是像这样传递参数

this.router.navigate(['tabs/gopremium', {accType: type}]);

应该这样通过

this.router.navigate(['/tabs/gopremium'], { queryParams: { accType: 'Platinum' }});

为了使平台准备好不会每次都触发,页面会被导航。


推荐阅读