【问题标题】:ionic 4, page transition hangs due to http call on ngOnInitionic 4,由于 ngOnInit 上的 http 调用,页面转换挂起
【发布时间】:2019-06-13 15:04:28
【问题描述】:

我在 ionic 4 中构建了一个应用程序。当从一个页面导航到另一个页面时,它并不是很流畅。

我正在使用routerLink="/make-order/{{item.name}}" routerDirection="forward" 前进到下一页。在 ngOnInit 函数的这个(下一个)页面上,我做了一些 http 调用来获取数据。问题是这对页面转换有影响。它不流畅,直到加载数据为止。我该如何解决这个问题?

【问题讨论】:

    标签: angular ionic4


    【解决方案1】:

    我有 2 个解决方法的想法: 首先,您可以将初始化代码移动到不同的(稍后)Angular 生命周期钩子。 https://angular.io/guide/lifecycle-hooks#lifecycle-examples

    可能是AfterViewAfterContent 或者使用 Ionic 的钩子:ionViewWillEnterionViewDidEnterionViewWillLeaveionViewDidLeave

    第二,您可以使用延迟运算符向可观察对象添加延迟。 https://www.learnrxjs.io/operators/utility/delay.html

    这应该允许在发出请求之前完成转换。

    进一步研究:

    我从 Ionic 文档中找到了这个

    Lifecycle Events
    With V4, we're now able to utilize the typical events provided by Angular. But for certain cases, you might want to have access to the events fired when a component has finished animating during it's route change. In this case, the ionViewWillEnter, ionViewDidEnter, ionViewWillLeave, and ionViewDidLeave have been ported over from V3. Use these events to coordinate actions with Ionic's own animations system.
    
    Older events like ionViewDidLoad, ionViewCanLeave, and ionViewCanEnter have been removed, and the proper Angular alternatives should be used.
    
    For more details, checkout the router-outlet docs
    

    https://beta.ionicframework.com/docs/building/migration#lifecycle-events

    【讨论】:

    • 后来的钩子有所帮助,但最终我禁用了过渡,现在它更流畅了
    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 2023-03-09
    • 2017-10-18
    • 2019-06-12
    • 2020-03-06
    • 2017-10-04
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多