更改路线时,视图不会滚动到angular2新页面的顶部 [英] On route change view doesn't scroll to top in the new page in angular2

查看:101
本文介绍了更改路线时,视图不会滚动到angular2新页面的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在路由器插座上尝试了 autoscroll = false ,但似乎不起作用,是否有任何默认方法
angular2在不使用任何第三方库的情况下进行相同操作?

I have tried autoscroll="false" on the router-outlet but does't seem to work, is there any default method of angular2 for doing the same without using any third party library?

推荐答案

在此处找到答案 https: //stackoverflow.com/a/39601987/5043867

我们可以订阅路线更改事件并滚动到顶部,并在

we can subscribe to the route change event and scroll to the top with something in the lines of

ngOnInit() {
    this.router.events.subscribe((evt) => {
        if (!(evt instanceof NavigationEnd)) {
            return;
        }
        document.body.scrollTop = 0;
    });
}

更新-

在Angular v6 +中,引入了新方法 scrollPositionRestoration 。有关更多信息,请在此处阅读

In Angular v6+ there is a new method scrollPositionRestoration introduced. for more info read out here

  • https://medium.com/@PardeepJain/deep-dive-into-angular-routing-scrolling-to-top-debugging-and-lot-more-a995c08498d3

这篇关于更改路线时,视图不会滚动到angular2新页面的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆