ionic 和 angular 与 ionic 生命周期之间的区别

Difference between ionic and angular and ionic lifecycles

我有一个显示城市的页面: 此页面使用名为 "CitiesProvider" 的提供程序 由于这些城市可能会改变,我把你的订阅如下:

this.citiesSubscription = this.CitiesProvider.salesChanged
  .subscribe(
    (cities: City[]) => this.cities = cities
  )

放置 "unsubscribe" 方法的最佳位置在哪里: ionViewWillLeave

ionViewDidLeave

ionViewWillUnload 如果我把它放在另一个里会出什么问题?

使用这些(Ionic)生命周期飞节或使用

有什么区别

ngOndestroy 来自 Anglar 或来自 angular 的任何飞节(因为离子页面也是 angular 组件

您应该使用 ngOnDestroyionViewWillUnload 取消订阅 Observables

每次您离开页面时,ionViewDidLeaveionViewWillLeave 挂钩都会被触发,但并不表示页面被销毁/从 dom 中删除。

示例

如果您使用 NavController 将页面推入堆栈,则会触发 ionViewDidLeave 而不是 destroy 挂钩,因为页面实际上停留在 dom 中。如果您向后导航,订阅仍会确保一切都是最新的。

如果您在离开页面时取消订阅,您需要在 ionViewWillEnter 生命周期挂钩中再次订阅。