使用 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更改导致页面重新加载
发布评论