Angular2组件加载XHR请求因为不包括虚拟目录作为Url的一部分而失败

时间:2016-12-10 10:27:39

标签: angular iis url-rewriting systemjs

我正在尝试使用gulp在system js builder的帮助下捆绑一个Angular 2应用程序。我接下来大多是用https://github.com/smmorneau/tour-of-heroes写的。我能够创建捆绑包。创建捆绑包后,我尝试在IIS中将应用程序部署为默认网站下的Web应用程序。 enter image description here

在我的应用程序中,我已将模板和css放在相应的组件文件夹中。但是在应用程序启动期间,我的html模板没有被加载,因为url不包含虚拟目录名并导致404.例如。 http://localhost/app/app.component.html应为http://localhost/Angular2Test/app/app.component.html。我的捆绑app.min.js请求此网址为xhr请求。为了解决正常的请求,我有IIS重写规则,如下所示

 <system.webServer>
    <rewrite>
      <rules>
        <rule name="angularjs routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/Angular2Test/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

有人可以帮我这个吗?我是否需要在Angular2端设置一些基本路径或其他东西。我已将我的示例上传到GitHub https://github.com/sajanep/Angular2PackagingTrial

1 个答案:

答案 0 :(得分:0)

我认为你需要

  • <base href="/someDir/">添加到<head>元素
  • 或者提供{provide: APP_BASE_HREF, useValue: '/someDir'}