当应用程序不在主机的根目录时,如何使Aurelia路由工作 [英] How can I make Aurelia routing work when the application is not at the root of the host

查看:66
本文介绍了当应用程序不在主机的根目录时,如何使Aurelia路由工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Aurelia应用程序,该应用程序将不在网站的根目录下运行.其URL将类似于 http://mycompany.com/neatoApp .

I am building an Aurelia application that will not live at the root of the web site. Its URL will be something like this http://mycompany.com/neatoApp.

我有一个看起来像这样的路由配置

I have a route configuration that looks like this

{route:['','index'], name:'index',moduleId:'index' nave:true, title: 'neato app home'},
{route:['news'], name:'news',moduleId:'news' nave:true, title: 'neato app news'}

我已经配置了System.js,以便它知道/neatoApp是baseURL,并且可以正确下载所有脚本和内容.我的应用html头中有一个< base href ="/neatoApp"/> 元素,并且我正在使用pushState进行路由.

I have System.js configured so that it knows /neatoApp is the baseURL and it downloads all the scripts and things properly. I have a <base href="/neatoApp" /> element in the head of my app html and I'm using pushState for routing.

问题是当我导航至mycompany.com/neatoApp时,Aurelia报告找不到路由:/neatoApp"

The problem is when I navigate to mycompany.com/neatoApp Aurelia reports 'Route not found: /neatoApp'

Router的baseUrl属性似乎与我设置的值无关,因为路由识别器不使用它.

Router has a baseUrl property that doesn't seem to matter what I set its value to because the route recognizer doesn't use it.

我真的不想将neatoApp放在我的所有路由中,这有几个很好的理由:随着该应用程序部署到各个地方(开发,测试等),基本路径将发生变化,这使维护工作变得很头疼.开发人员不知道要在各种环境中部署该应用程序的位置,也不是操作人员的责任,而是在他部署该应用程序时更新代码以包括该基本URL.当我在路由配置中确实包含neatoApp时,它会使导航的行为异常,就像生成指向/neatoApp/neatoApp的链接一样.

I really don't want to put neatoApp in all my routes for a few good reasons: As the app gets deployed in various places (dev, test, etc) that base path will change making that a headache to maintain. It's not the developers responsibility to know where the app is going to be deployed in various environments and it's not the operations guy's responsibility to update the code to include that base URL when he deploys it. When I do include neatoApp in the routing config it makes navigating behave strangely like generating a link that points to /neatoApp/neatoApp.

有人可以解决这个问题吗?

Does anybody have a solution to this problem?

我创建了一个插件来演示该问题: http://plnkr.co/edit/HPEzClAlncvQkSzjw6ph?p =预览

I have created a plunker to demonstrate the issue: http://plnkr.co/edit/HPEzClAlncvQkSzjw6ph?p=preview

推荐答案

对于我的MVC 5应用程序,我在Index.cshtml中使用它:

For my MVC 5 app, I use this in my Index.cshtml:

<script src="~/jspm_packages/system.js"></script>
<script>
    System.config({ baseURL: "@Url.Content("~/")" });
</script>
<script src="~/config.js"></script>
<script>
    System.import('aurelia-bootstrapper');
</script>

并注释掉了config.js中的baseUrl.需要在对System.config()的第一次调用中设置BaseUrl.

And have commented out the baseUrl in config.js. BaseUrl needs to be set in the first call to System.config().

System.config({
  //baseURL: "./",
  defaultJSExtensions: true,
  ...

这篇关于当应用程序不在主机的根目录时,如何使Aurelia路由工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆