Angular - 仅在一条路线上滚动位置恢复
我想在一页上禁用scrollPositionRestoration。假设我的 app-routing.module.ts 文件中有以下路由...
const appRoutes: Routes = [{ path: 'home', component: myComponent}, { path: 'about', component: myComponent}, ]
并且我的导入如下...
@NgModule({imports: [RouterModule.forRoot(appRoutes, {
scrollPositionRestoration: 'enabled'
})]
如何禁用“about”路由上的 scollPositionRestoration?
I want to disable scrollPositionRestoration on one page. Let's say I have the following routes in my app-routing.module.ts file...
const appRoutes: Routes = [{ path: 'home', component: myComponent}, { path: 'about', component: myComponent}, ]
and I have my imports as follows...
@NgModule({imports: [RouterModule.forRoot(appRoutes, {
scrollPositionRestoration: 'enabled'
})]
How would I disable the scollPositionRestoration on the 'about' route?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用:
scrollPositionRestoration: 'enabled'
重置回:
然后仅在 home.component.ts 中使用它:
然后添加
(click)="scrollTop()"
你想在哪里执行它。Do not use:
scrollPositionRestoration: 'enabled'
Reset back to:
Then use this in your home.component.ts only:
Then add
(click)="scrollTop()"
where you want to execute it.您无法在不同的路由中以不同的方式控制
scrollPositionRestoration
值。此选项仅适用于根模块。因此,在您的情况下,您应该从根模块中删除它(默认情况下scrollPositionRestoration
值为'disabled'
),并且可以通过Router 控制滚动的位置
和ViewportScroller
。在
app.component
中,您可以监听这样的导航事件:You could not control
scrollPositionRestoration
value differently in different routes. This option for root module only. As a result, In your case you should remove it from root module (by defaultscrollPositionRestoration
value is'disabled'
) and can control the position of scroll byRouter
andViewportScroller
.In the
app.component
You can listen navigation events like this: