Closed Ajax-ua closed 4 years ago
@Ajax-ua Do you have
@ViewChild('tabs', { static: true, read: IonTabs }) private tabsRef: IonTabs;
as a local property of the component for that template ? Also just a general tip, avoid binding functions to templates directly, it causes a lot of calls when the view gets repainted :)
@NikolaPeevski I didn't have local property via ViewChild decorator and it works correctly on Ionic 4. However I've tried to use it with Ionic 5 but I still get the same error
@Ajax-ua Can you provide a repo that consistently yields this problem?
@NikolaPeevski please check: https://github.com/Ajax-ua/ionic5-tabs
By the way, when I call this method in component then there is no error but it still doesn't work and returns undefind
.
@Ajax-ua Okay, so the way tabs work is that they use a stack controller to retain tab history. This controller gets initialised when an component with a ion-tabs is created. Since you're binding the getSelected() to either the template/ngOnInit you're getting this issue because you're trying to call getSelected() while the controller is being initialised. It does not take much to initialise, it's quite instant, but in this instant the tabs html page and it's component are also rendered/created. If you were to write
setTimeout(() => {console.log(this.tabsRef.getSelected())}, 1500)
in the ngOnInit of the tabs page you'll get your result. Of course this is an over-exaggerated timeout value, you could try ngAfterViewChecked instead if you want, but I hope it clarifies the confusion. You can read a little bit more about lifecycle hooks here.
@NikolaPeevski thanks for your answer! Calling method in ngAfterViewChecked returns undefined. As well as in ngAfterViewInit hook. I've achieved an expected result using ionViewDidEnter hook. So I can solve my problem using it. However, I think that the tabs behavior isn't clear and it should be at least described in the documentation
You can rename this issue and/or close it and reference it in a new issue here https://github.com/ionic-team/ionic-docs since it's not a bug.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.
Bug Report
Ionic version:
[x] 5.x
Current behavior:
Using getSelected() method of ion-tabs component throws an error "Cannot read property 'getActiveStackId' of undefined"
Expected behavior:
getSelected() method of ion-tabs component should return selected tab
Steps to reproduce:
Use ion-tabs component and call getSelected() method.
Related code:
Other information:
Ionic info: