Angular - 无法使用“scrollPositionRestoration”

mhf*_*our 4 angular

我目前使用的是 Angular 6.1.5。当网站路由到不同页面时,我希望页面滚动回顶部。我尝试使用{ scrollPositionRestoration: 'top' }但它不起作用。

这是我在 app.routing.ts 中的代码。

export const routing = RouterModule.forRoot(appRoutes, { scrollPositionRestoration: 'top' });
Run Code Online (Sandbox Code Playgroud)

有替代方案吗?我尝试从“顶部”更改为“启用”,但效果不佳。我也尝试window.scrollTo(0,0);将其放入ngOnInit(),但也不起作用。

小智 8

就我而言,这不起作用,因为我的 CSS 中有这个。

@media (prefers-reduced-motion: no-preference) {
  :root {
    scroll-behavior: smooth;
  }
}
Run Code Online (Sandbox Code Playgroud)

我改为

scroll-behavior: auto;
Run Code Online (Sandbox Code Playgroud)

然后它开始工作