首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角路由-将url重定向到组件

角路由-将url重定向到组件
EN

Stack Overflow用户
提问于 2017-11-29 08:34:24
回答 3查看 2.2K关注 0票数 0

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

代码语言:javascript
复制
<a [routerLink]="'dashboard'">Dashboard</a>

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

代码语言:javascript
复制
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/仪表板,生产中是server.com/appname/dashboard?时,会出现问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-11-29 08:49:07

我找到了原因。我必须配置退路。这里是一篇关于它的文章。

我不得不添加web.config:

代码语言:javascript
复制
<?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。

票数 0
EN

Stack Overflow用户

发布于 2017-11-29 08:48:36

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

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

动态设置基座href -角2/4

票数 1
EN

Stack Overflow用户

发布于 2017-11-29 08:37:50

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

代码语言:javascript
复制
<base href="/appname/">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47548281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档