I'm facing an issue where after using router.navigateByUrl(""), I need to scroll back to the top.
I've experimented with various methods, but none of them seem to be effective:
- window.scroll(0, 0);
- document.body.scrollTop = 0;
- document.querySelector('body').scrollTo(0, 0);
- NbLayoutScrollService.scrollTo(0, 0); // Scroll service from Nebular
- viewportScroller.scrollToPosition([0, 0]);
- document.querySelector().scrollTo(0, 0); /* It has been noted that Angular 8 with material may have issues with scrolling if the hook is not on a different HTML element than the body */
I attempted to use these methods within this.router.events.subscribe((evt) => { ... }; as well as with the following approach:
<router-outlet (activate)="onActivate($event)"></router-outlet>
onActivate(event) {
...
}
Router events did not trigger at all, but the second method did. However, the scrolling mechanisms still failed to work.
I received a warning stating "A router outlet has not been instantiated during routes activation. URL Segment:", could this be the reason why scrolling isn't functioning?
The application is utilizing Angular Version 10.1.1, any suggestions or insights on troubleshooting this issue would be greatly appreciated as I'm currently stuck.