简体   繁体   English

Angular JS IE9 Hashbang URL重写

[英]Angular JS IE9 Hashbang url rewriting

Quite an odd situation here. 这里的情况很奇怪。

So I have two routes: /RouteA ( / also defaults to here) and /RouteB 所以我有两条路线: /RouteA/也默认为这里)和/RouteB

/RouteA actually uses client side routing while /RouteB does not if that matters. /RouteA实际上使用客户端路由,而/RouteB这样做。

When I try navigating to /RouteB , the URL is initially /RouteB which is correct. 当我尝试导航到/RouteB ,URL最初是/RouteB ,这是正确的。

Since IE9 doesn't support HTML5, Angular rewrites the url with a hashbang to /#/RouteB 由于IE9不支持HTML5,因此Angular用hashbang将网址重写为/#/RouteB

When the rewriting occurs, it forces a browser refresh (can we stop this behavior?) which sends me right back to / (which is ultimately /RouteA . 重写发生时,它会强制浏览器刷新(我们可以停止这种行为吗?),这会将我直接发送回/ (最终是/RouteA

Is there a way to prevent the browser from resending the request when the url is rewritten? 有没有办法防止在重写URL时浏览器重新发送请求?

The base tag is currently <base href="/"/> . 基本标记当前为<base href="/"/>

I tried doing something like this which allows me to land on the page, but breaks the anchor tag in the main menu thats trying to send me back to /RouteA : 我试图做这样的事情,使我能够进入页面,但在试图将我发送回/RouteA的主菜单中破坏了定位标记:

  if (window.history && window.history.pushState) {
     $locationProvider.html5Mode(true);
  }
  else {
     window.location.hash = '/';       
     $locationProvider.html5Mode(false);
  }

eg /#/RouteB becomes /RouteB/#/ . 例如/#/RouteB变为/RouteB/#/ Then clicking on a link back to /RouteA , the route ends up becoming /RouteB/RouteA which obviously breaks. 然后单击返回到/RouteA的链接,该路由最终变成/RouteB/RouteA ,这显然会中断。

So I tried the solution from a similar question but it yielded the same results as above. 因此,我尝试了一个类似问题的解决方案,但得出的结果与上述相同。 $location / switching between html5 and hashbang mode / link rewriting $ location /在html5和hashbang模式之间切换/链接重写

Does anyone have any ideas? 有人有什么想法吗?

Thanks in advance! 提前致谢!

I managed to get it working. 我设法使它起作用。 It isn't pretty, but since there is no client side routing happening in /RouteB so it is good enough. 它不是很漂亮,但是由于/RouteB没有发生客户端路由,因此已经足够了。

Before the two different apps shared an angular module. 在两个不同的应用共享一个角度模块之前。 Instead I created two different modules and had those depend on the previously shared module. 相反,我创建了两个不同的模块,并使它们依赖于先前共享的模块。

angular.module('sharedModule', ['dependancy1', ...]);

<!-- RouteA app -->
<div ng-app="sharedModule">...

<!-- RouteB app -->
<div ng-app="sharedModule">...

Became 成为

angular.module('sharedModule', ['dependancy1', ...]);
angular.module('routeAModule', ['sharedModule']);
angular.module('routeBModule', ['sharedModule']);


<!-- RouteA app -->
<div ng-app="routeAModule">...

<!-- RouteB app -->
<div ng-app="routeBModule">...

Then in routeBModule I used the fix from this post: $location / switching between html5 and hashbang mode / link rewriting that was also mentioned above in my question. 然后在routeBModule我使用了这篇文章中的修复方法: $ location /在html5和hashbang模式之间切换/链接重写 ,上面在我的问题中也提到过。

Now when navigating to /RouteB the route gets rewritten to /RouteB#/ which wont reload / and dumping me back a different page. 现在,当导航到/RouteB ,路由将被重写为/RouteB#/ ,它不会重新加载/并将我转储到另一个页面。

There were two gotcha's with this solution. 这个解决方案有两个陷阱。

  1. Navigating to this page with a trailing / can mess up relative links. 尾随/导航到此页面可能会使相关链接混乱。 eg /RouteB#/ becomes /RouteB/#/ so trying to link to /NewRoute would end up linking to /RouteB/NewRoute 例如/RouteB#/变为/RouteB/#/因此尝试链接到/NewRoute最终将链接到/RouteB/NewRoute
  2. Anchor tags on this page with a href="#" cause the page to refresh. 此页面上带有href="#"定位标记会导致页面刷新。

There are likely many improvements that can be made to this and if I make them ill try to remember to update this answer. 可能可以对此进行许多改进,如果我不满意,请尝试记住更新此答案。

Also if anyone has a better solution please feel free to post it. 另外,如果有人有更好的解决方案,请随时发布。

<3 IE9 <3 IE9

What do your links to the routes look like? 您到路线的链接是什么样的? I think you need a full path to any routes going outside your application. 我认为您需要一条通往您应用程序之外的任何路由的完整路径。 You might try something like: 您可以尝试以下方法:

<a href="RouteA">Route A</a>
<a href="/RouteB">Route B</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM