Angular 4 - RouterOutlet 404 错误

Angular 4 - RouterOutlet 404 Error

我的 angular 4 应用程序托管在 'Website' 中作为 IIS 中的 'Application' 让我们说 'MyApp' 并且我使用 [=34= 生成了生产版本]-cli 使用此命令:

ng build --prod --base-href=/MyApp/

我还在 IIS 中配置了 Url 重写,下面是 index.html

旁边的 web.config 文件
<configuration>
  <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" />
          </conditions>
          <action type="Rewrite" url="/MyApp/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

以下是我的主要应用程序路由,它基本上具有不同模块的入口点和它自己的路由:

@NgModule({
  imports: [
    RouterModule.forRoot([
      { path: 'vm', redirectTo: 'vm/vehicles', pathMatch: 'full' },
      { path: 'rp', redirectTo: 'rp/gantt', pathMatch: 'full' },
      { path: '', redirectTo: 'rp/gantt', pathMatch: 'full' },
      { path: '404', component: PageNotFoundComponent },
      { path: '**', redirectTo: '404' }
    ]
    )
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule { }

问题是如果我使用此 url http://localhost:8181/MyApp/vm the app loads fine but the <router-outlet></router-outlet> which has to render the component configured against vm/vehicles doesn't load and it shows me 404 component inside the <router-outlet> and the url changes to http://localhost:8181/MyApp/404.

访问应用程序

以下是 vm 模块的路由:

const moduleName = 'vm';

const routes: Route[] = [
      { path: 'vehicles', component: VehicleComponent },
      { path: 'standards', component: StandardsComponent }
    ];

 routes.forEach((item) => {
      item.path = moduleName + '/' + item.path;
  });

@NgModule({
  imports: [
    RouterModule.forChild(routes)
  ],
  exports: [
      RouterModule
  ]
})
export class VehicleRoutingModule { }

问题实际上是这部分代码:

 routes.forEach((item) => {
      item.path = moduleName + '/' + item.path;
  });

我不知道确切原因,因为我们只是动态更改路径但 angular 不接受它,以下代码有效:

const routes: Route[] = [
      { path: 'vm/vehicles', component: VehicleComponent },
      { path: 'vm/standards', component: StandardsComponent }
    ];