在 Ionic4 的主页内初始化组件
Initializing component inside a home page in Ionic4
我有一个主页,我在其中创建了一个组件RecentlyViewedProductComponent
。
我的问题是:
当我使用
导航到/home
时
this.router.navigate(['/home']);
RecentlyViewedProductComponent
中的 ngOnInit()
不工作。当我关闭应用程序并再次打开它时,只有它在工作。
如何解决这个问题?
听起来您没有使用正确的生命周期事件。
你看过这里的文档了吗:
它说 ngOnInit()
是:
Fired once during component initialization. This event can be used to initialize local members and make calls into services that only need to be done once.
如果您希望每次导航到主页时都调用它,那么您需要将其替换为 ionViewWillEnter()
:
Fired when the component routing to is about to animate into view.
实际上 some guidance at the end of the docs page 您可能会觉得有趣,它解释了何时使用每种生命周期方法。
我有一个主页,我在其中创建了一个组件RecentlyViewedProductComponent
。
我的问题是:
当我使用
导航到/home
时
this.router.navigate(['/home']);
RecentlyViewedProductComponent
中的 ngOnInit()
不工作。当我关闭应用程序并再次打开它时,只有它在工作。
如何解决这个问题?
听起来您没有使用正确的生命周期事件。
你看过这里的文档了吗:
它说 ngOnInit()
是:
Fired once during component initialization. This event can be used to initialize local members and make calls into services that only need to be done once.
如果您希望每次导航到主页时都调用它,那么您需要将其替换为 ionViewWillEnter()
:
Fired when the component routing to is about to animate into view.
实际上 some guidance at the end of the docs page 您可能会觉得有趣,它解释了何时使用每种生命周期方法。