Angular2 pathLocationStrategy:URL更改导致页面重新加载

编程入门 行业动态 更新时间:2024-10-25 04:18:56
本文介绍了Angular2 pathLocationStrategy:URL更改导致页面重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 HashLocationStrategy 时,我可以通过以下方式更改路线无需在不重新加载页面的情况下手动更改浏览器地址栏中的地址. IE.从mysite/#/home导航到mysite/#/profile

While using HashLocationStrategy I can change route by changing address in browser's address bar by hand without page reload. I.e. navigation from mysite/#/home to mysite/#/profile

但是,如果我使用 PathLocationStrategy (它是默认的位置策略),当我尝试执行相同操作时,我有不必要的页面重新加载. IE.从mysite/home导航到mysite/profile

However, if I use PathLocationStrategy (it is default location strategy), I have unwanted page reload, when I try to do the same thing. I.e. navigation from mysite/home to mysite/profile

是否可以解决此问题?

我正在使用Angular 2.0.0-beta17

I am using Angular 2.0.0-beta17

推荐答案

这是按设计的".当您仅更改#...时,没有任何内容可发送到服务器. #...部分始终仅由浏览器处理,并且从未发送到服务器.

That's "as designed". When you only change the #... then there is nothing to send to the server. The #... part is always only processed by the browser and never sent to the server.

如果您更改#之前的部分,并且如果没有#,则所有内容都不是-#-部分,那么浏览器需要向服务器提出新请求以获取网址.

When you change the part before #, and if you don't have a # than everything is the before-#-part then the browser needs to make a new request to the server to fetch the URL.

如果您使用window.history... API( developer .mozilla/zh-CN/docs/Web/API/History_API ),然后您告诉浏览器只更新URL栏,而不调出服务器. Angular路由器使用此API,因此它可以在应用程序内使用,或者在您使用后退或前进按钮时有效,而在您手动更改URL时无效.

If you use the window.history... API (developer.mozilla/en-US/docs/Web/API/History_API) then you tell the browser to just update the URL bar but don't call out to the server. The Angular router uses this API therefore this works from within the app or when you use the back or forward button but not when you manually change the URL.

更多推荐

Angular2 pathLocationStrategy:URL更改导致页面重新加载

本文发布于:2023-11-01 19:58:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   页面   pathLocationStrategy   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!