[英]Angular 2+ (v11) automatically encode fragment hash (#) in URL into %23
My Angular app has to automatically scroll (only Y-axis) to a certain component when a corresponding hash exists in the URL.当 URL 中存在相应的 hash 时,我的 Angular 应用程序必须自动滚动(仅 Y 轴)到某个组件。
Eg url.com/page1#element1
should make the browser automatically scrolls to Element 1, naturally with #element1
inside the HTML tag.例如
url.com/page1#element1
应该使浏览器自动滚动到元素 1,自然而然地在 HTML 标签内使用#element1
。
But somehow my Angular app automatically replaces the hash (#) with %23, its URL-encoded version.但不知何故,我的 Angular 应用程序自动将 hash (#) 替换为 %23,它的 URL 编码版本。 This causes the problem that my app cannot recognise any fragment in the URL (I use
this.activatedRoute.fragment.subscribe()
to get the fragment value) I tried checking everywhere in the code, but I can't seem to find the source of the problem.这导致我的应用程序无法识别 URL 中的任何片段的问题(我使用
this.activatedRoute.fragment.subscribe()
来获取片段值)我尝试在代码中到处检查,但似乎找不到源的问题。 Does anybody have any possible solution to this?有人对此有任何可能的解决方案吗? Thank you!
谢谢!
Not really a fix, but I think this is a problem specific to my app.不是真正的修复,但我认为这是我的应用程序特有的问题。 So I basically have 2 mode of my app, one is the normal
ng serve
and the other one is SSR mode (server side rendering).所以我的应用程序基本上有 2 种模式,一种是普通的
ng serve
,另一种是 SSR 模式(服务器端渲染)。 Basically this problem only shows up in the normal mode, but if I run the app with the SSR mode (also what I have in prod), the hash is being kept as-is.基本上这个问题只出现在正常模式下,但如果我以 SSR 模式运行应用程序(也是我在产品中所拥有的),hash 将保持原样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.