I have got a problem with the subscription in Ionic 4 page. The subscribe method is just being fired when I load the page through its route directly in the browser. If I navigate to the page throughout a router link the subscribe method is not fired.
I am using Ionic Tabs and read a few GitHub issues that state that the lifecycle events are not working correctly between the tabs navigation.
Please see the following code from my page (score.page.ts
):
ngAfterViewInit() {
console.log('ngAfterViewInit');
this.productService.points.subscribe(
(e) => console.log('score points', e)
);
}
ionViewDidLoad() { console.log('ionViewDidLoad'); }
ngOnInit(): void {
console.log('ngOnInit');
}
ionViewWillEnter() { console.log('ionViewWillEnter'); }
ionViewDidEnter() { console.log('ionViewDidEnter'); }
ionViewWillLeave() { console.log('ionViewWillLeave'); }
ionViewDidLeave() { console.log('ionViewDidLeave'); }
ionViewWillUnload() { console.log('ionViewWillUnload'); }
ngOnDestroy() {
console.log('ngOnDestroy');
}
Console output with directly loaded /app/home/score
:
As you can see, the lifecycle events are working correctly and the subscribe method has been called as excpected.
As I load /app/home
and navigate to /app/home/score
(I tried a routerlink and router.navigate), the console log of the subscription is missing:
This is a huge problem since I need to display data dynamically on the different pages, and this is not possible if I cannot subscribe to an Observable from my service.
I also tried {{ productService.points | async }}
in the template since then angular deals with the subscription itself, but the error stays the same.
productService.points
is of type Observable<Number>
but the error occurs on other Observables too.
Should I file an issue for that or am I just missing something?
Please comment if you need something else.
Thanks in advance