当点击我的网站内的任何链接,我的服务,他们工作。但是,在使用ng build之后,所有页面链接都不起作用。网站是:hiphost.co.za,如果你想自己测试一下的话。
以下是我的路由器的代码:
RouterModule.forRoot([
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'home',
component: HomeComponent,
pathMatch: 'full'
},
{
path: 'terms-and-conditions',
component: TermsAndConditionsComponent
},
{
path: 'privacy',
component: PrivacyPolicyComponent
},
{
path: 'about',
component: AboutComponent
},
{
path: 'contact',
component: ContactComponent
},
{
path: 'team',
component: TeamComponent
},
{
path:'safety',
component: SafetyComponent
}
]
)
因此,当您转到hiphost时,它会自动重定向到/home,然后那里的链接将不起作用
发布于 2018-09-03 20:08:00
所有的路由请求都是由服务器处理的,而不是你的应用程序,因此给出了404。
最简单的解决方案是引导你的服务器将你的所有请求重定向到index.html,这样你的angular路由器就可以处理它。
发布于 2018-09-03 20:36:35
在生产环境中,angular应用程序由web服务器托管。因此,当您重定向到任何嵌套视图时。web服务器无法识别路径和抛出404错误。请点击此链接,了解如何告诉web服务器导航到嵌套视图。
发布于 2018-09-03 22:50:26
您需要在web.config文件中包含以下内容
<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" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
您还需要在您的index.html上添加此代码。
<base href="/">
https://stackoverflow.com/questions/52149420
复制相似问题