angular - 每次通过路由器导航页面时,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
?
解决方案
我找到了答案,
而不是像这样传递参数
this.router.navigate(['tabs/gopremium', {accType: type}]);
应该这样通过
this.router.navigate(['/tabs/gopremium'], { queryParams: { accType: 'Platinum' }});
为了使平台准备好不会每次都触发,页面会被导航。
推荐阅读
- python - 如何在 Keras 模型的自定义损失中访问张量的内容
- python - Python Wand - 作物比例?
- python - 在 NumPy 中计算一个序列的经验分布?
- r - purrr:按(巢)和引导分组
- r - 如果数据框列中的变量名称与向量重命名变量中的名称匹配
- sql-server - SQL Server 聚集索引
- jenkins - Jenkins - 无法使用“构建作业”命令访问从另一个构建管道中的一个构建管道发送的参数
- javascript - 谷歌地图 fitBounds 无法在模式内的地图上工作
- javascript - 使用 websocket 将客户端连接到服务器
- javascript - 带有addPage的jsPDF html方法在生成的PDF中拆分页面