@Corin I don’t know when you are calling the content method in your code, but did you put that call inside ngOnInit, ngOnChanges or something like that? Maybe the view was not initiated yet. Try to use only after the view was loaded (after ngAfterViewInit was called).
@Corin Content should be defined. Are you using @ViewChild(Content) in the same component that has the ion-content tag in the html template? (that is, it’s not a super-component nor a sub-component)
As I understand it, this timing issue is related to the static property of @ViewChild, which didn’t exist when this thread was created, but is important now and will be mandatory going forward.
I am experiencing the same in Ionic 6, but I’m wondering if the only solution to implement something like scrollToTop() is to create one dummy function ionViewDidEnter() which is scrolling to top (just to initialize such content variable) or if we have one more solution.
@flavio_b
Yes, it is the best way to implement that function to solve the solution.
Although it is not the best practice (in my opinion), you may also write asynchronous functions in the background and trigger it before landing on the page.
Hi! Thank you for replying.
Unfortunately this didn’t solve for me totally.
Such function ionViewDidEnter() makes the content variable no longer undefined now:
Also, the way I recommended for ionic3 might be deprecated in ionic 6. Therefore, you should use the coding rules in the new document. I will share the link which might help you.
Thanks!!
I’ve been in that documentation page for at least 3/4 days so far.
For what I see, it suggests to add the following to the ion-content and nothing more:
And this is working to me, if I add all those three methods to my .ts file.
But the documentation doesn’t say anything about handling the button event and triggering the scrollToTop() function that should be something like this:
The thing is that such scrollToTop() function is not a function for the framework! Why? Now that the this.content is not undefined, I should be able to call that function.
Also, for what I see here, I should be able to call scrollToTop() but it is not true.
I think I’ve found out why it was not working so far.
I have solved adding FormsModule and IonicModule to the imports section of the module.ts page module class: