混合使用 Angular2、Backbone 和 MVC 路由

Mixing Angular2, Backbone and MVC routing

我们有一个现有的 MVC5 网络应用程序,大部分使用 Backbone 和 Backbone 路由。当您到达该站点时,它会加载原始的基本 home/index.html,然后加载所有 Backbone 脚本等

我们正在引入 Angular2(版本:2.0.1),并为 Angular 从我们的 NgController 提供一个新的 index.html 页面。

目前,我们有使用Hash定位策略的应用;应用程序加载后,路由将控制 URL,我们从以下内容开始:

http://blah.com/ng/#/reports

http://blah.com/#/reports

这通常没问题,在 angular 应用程序中四处浏览会将 url 更改为 /#/pageXyz 等。 ...直到您尝试直接刷新或深入 link 到后面的示例,其中浏览器在技术上只是去

http://blah.com

其中提供原始 Backbone 索引页。

有哪些解决方案?

一夜之间,Angular 站点似乎获得了设置 VS2015 的 incomplete teaser page(注意:我们目前使用的是 VS2013),并且在最底部让您陷入看似相关的悬崖峭壁问题:

Note on Routing Applications

If this application used the Angular router, a browser refresh could return a 404 - Page Not Found. Look at the address bar. Does it contain a navigation url (a "deep link")? We'll have to configure the server to return index.html for these requests. Until we do, remove the navigation path and refresh again.

提前致谢。

最终更改了 Backbone 路由。

优点:有效且只有两行:

#restore angular url
if location.pathname == '/' and location.hash.substring(0,2) == '#/'
    location.href = location.href.replace('/#/','/ng/#/')

缺点:在 refresh/deep link 上,您会加载两个完整页面 - 首先是 home/index 加载 backbone,然后是 ng/index 加载 angular 重定向后。

尽管如此,考虑到所有因素,它的实际效果对我来说已经足够好了。