2017-08-24 87 views
1

我的问题是,我每次加载我的链接。例如: “https://www.example.com/”它将重定向到 “https://www.example.com/design-brief”,因为我在我的routing.ts中配置了它。 但是,当我使用链接“https://www.example.com/design-brief”并进入我的地址栏时,它将在控制台日志中显示错误。 “GET https://www.example.com/design-brief 404()”Angular Router获取404错误?

const APP_ROUTES: Routes = [ 
{ path: 'design-brief', component: DesignBriefComponent }, 
{ path: 'design-evaluation', component: DesignEvaluationComponent }, 
{ path: '', redirectTo: '/design-brief', pathMatch: 'full' }, 
{ path: "**",redirectTo:"/design-brief" } ]; 

这是我在我的routing.ts

回答

2

前斜杠(/)查看my answer here对于为什么THI更详细的解释s正在发生。

概括地说,你有两个选择:

  1. 配置你的Web服务器始终与 的index.html回应时,它检测到404 - 这样,角永远 负载和路由服务将处理 客户端的导航。

  2. 将您的应用的位置策略更改为哈希位置策略 描述为here。但是,这会改变您的应用的网址,并且在我看来这不是我想要的 。

+0

It解决了我的问题。谢谢。 顺便说一句,有没有办法删除“#”?我的意思是,有没有办法解决它而不使用散列? – LordGrim

+0

干净的方法是使用默认位置策略(所以不是哈希),并在Web服务器上进行一些配置,正如我所提到的。如果你确实使用HashLocationStrategy,那么#将留在:) –

+0

如何配置它?我只是使用github(angular-cli-ghpages),以便我可以看到我的角色应用程序正在运行。这只是为了我的练习。但我想学习干净的方式或正确的方式。 – LordGrim

0

代码不需要空路径和**路,只有pathMatch的**路径“全“:

const APP_ROUTES: Routes = [ 
{ path: 'design-brief', component: DesignBriefComponent }, 
{ path: 'design-evaluation', component: DesignEvaluationComponent }, 
{ path: "**",redirectTo:"design-brief",pathMatch: 'full' } ]; 

和重定向并不需要在设计简要

+0

但如果我删除它,页面不会重定向到“设计简要”页面。我正在使用angular-cli-ghpages。当我使用链接“https://www.example.com/design-brief”时,它仍然在我的控制台日志中显示“GET https://www.example.com/design-brief 404()” – LordGrim

0

我通过使用Hash位置解决它strategy.Add下面的代码到@ngModule的进口部分,并用# 前缀的所有超链接,也指offecial link的信息

RouterModule.forRoot(
    appRoutes,{ useHash: true } 
)