0

我在生产模式下重定向我的应用程序时遇到问题。当我有 url 时http://server.com/projectname/dashboard,服务器响应 IIS 错误页面 404。我必须使用打开应用程序http://server.com/projectname,然后单击链接,例如:

<a [routerLink]="'dashboard'">Dashboard</a>

在 html 我有<base href="./">,这是我的路由器:

const appRoutes: Routes = [
    { path: '', redirectTo: 'dashboard', pathMatch: 'full', canActivate: [AuthGuard] },
    { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard]},
    { path: '400', component: ErrorComponent, canActivate: [AuthGuard] },
    { path: '401', component: ErrorComponent, canActivate: [AuthGuard] },
    { path: '404', component: ErrorComponent, canActivate: [AuthGuard], 
    { path: '**', redirectTo: '/404' }
]

@NgModule({
    imports: [
        RouterModule.forRoot(appRoutes)
    ]
})

在开发模式下,我有 url http://localhost:4200/dashboard,我应该重定向。在开发中是 url localhost:4200/dashboard 而在生产中是 server.com/ appname /dashboard 会不会有问题?

4

3 回答 3

1

您需要更新应用程序的基本 href。如果您正在使用,我不会推荐@Daniel 的答案angular-cli,因为有一种更简单的方法。

使用angular-cli,您可以使用:ng build --prod --bh /appname/这将更新生产构建基础 href。这是一种更好的方法,因为您不必index.html在环境之间切换时手动更新它。

动态设置基本 href - Angular 2 / 4

于 2017-11-29T08:48:36.993 回答
0

在生产环境中部署时,在 index.html 中添加这一行:

<base href="/appname/">
于 2017-11-29T08:37:50.043 回答
0

我找到了原因。我必须配置回退。是关于它的文章。

我必须添加 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
   <rewrite>
    <rules>
      <rule name="Angular 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="./" />
      </rule>
    </rules>
  </rewrite>
  </system.webServer>
</configuration>

现在它可以正常工作了,我可以打开任意应用程序 url。

于 2017-11-29T08:49:07.527 回答