【问题标题】:Angular 4 - RouterOutlet 404 ErrorAngular 4 - RouterOutlet 404 错误
【发布时间】:2018-07-18 22:18:28
【问题描述】:

我的 Angular 4 应用程序作为 IIS 中的“应用程序”托管在“网站”内,比如说“MyApp”,我已经使用 angular-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 访问应用程序,应用程序加载正常,但必须呈现针对 vm/vehicles 配置的组件的 &lt;router-outlet&gt;&lt;/router-outlet&gt; 不会加载,它在 @ 内显示 404 组件987654330@ 和 url 更改为 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 { }

【问题讨论】:

  • 在开发模式下对你有用吗

标签: angular routing angular4-router


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2014-12-21
    • 2013-12-08
    相关资源
    最近更新 更多