Angular 4路由不适用于prod

时间:2018-04-10 07:33:56

标签: angular routing angular-routing angular-router

我正在开发一个角度4的网络应用程序,并像这样做路由:

const routes: Routes = [
    { path: '', redirectTo: "raids", pathMatch: 'full' },
    {
        path: 'raids',
        component: RaidsContainerComponent
    },
    {
        path: 'gyms',
        component: GymsContainerComponent
    },
    {
        path: 'raid/:rid',
        component: RaidDetailsComponent
    },
    {
        path: 'user',
        component: UsersContainerComponent
    },
    {
        path: 'register',
        component: RegisterComponent
    }, 
    { path: '**', redirectTo: "raids", pathMatch: 'full' }
];

当我在本地主机上并且我在url行localhost / raids中写道时,它会将我带到RaidsContainerComponent,但是当我在发布应用程序后在生产中执行相同操作时,我得到:

  

您要查找的资源已被删除,名称已更改或暂时不可用。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

好的我已经弄明白了,为了将我的网站发布到azure,我必须制作web.config文件:

<?xml version="1.0"?>
<configuration>
<system.webServer>
<staticContent>
  <mimeMap fileExtension=".json" mimeType="application/json" />
  <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
  <mimeMap fileExtension=".woff2" mimeType="font/woff2" />
</staticContent>
</system.webServer>
<system.webServer>
<rewrite>
  <rules>
    <rule name="angular cli 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="/index.html" />
    </rule>
  </rules>
</rewrite>
</system.webServer>
</configuration>

但ng-build没有将它添加到dist文件中,所以我不得不手动将它添加到ftp服务器,现在它正在工作。