Angular 8:在子组件中选择浏览器后退按钮时恢复滚动位置 [英] Angular 8: Restore scroll position when browser back button is selected in child component

查看:24
本文介绍了Angular 8:在子组件中选择浏览器后退按钮时恢复滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多卡片列表的组件(如网格格式).向下滚动并选择其中一张卡片后,我希望在按下浏览器后退按钮时返回到相同的滚动位置.

I have a component consisting of a list of many cards (like a grid format). Upon scrolling down and selecting one of the cards, I would expect to return to the same scroll position when I press the browser back button.

我无法使用 Router 的 scrollPositionRestoration 方法,因为它位于我的子组件中.

I'm unable to use Router's scrollPositionRestoration method as it is located in my child component.

感谢您的帮助!

推荐答案

我能够通过添加

{ scrollPositionRestoration: 'top' } 到 RouterModule.forRoot(routes) 像这样:

{ scrollPositionRestoration: 'top' } to the RouterModule.forRoot(routes) like this:

RouterModule.forRoot(routes, { scrollPositionRestoration: 'top' })

它 100% 有效

这篇关于Angular 8:在子组件中选择浏览器后退按钮时恢复滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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