Angular 6构建,刷新页面时未找到错误404 [英] Angular 6 build, Error 404 Not Found when refresh page

查看:113
本文介绍了Angular 6构建,刷新页面时未找到错误404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刷新页面时,我在生产版本中遇到 404页面未找到的问题. 使用#路由RouterModule.forRoot(routes, { useHash: true })可以正常工作.但是我想要一条没有#

I have facing a problem 404 page not found in production build when refresh a page. Build working fine with # routing RouterModule.forRoot(routes, { useHash: true }). But I want a pretty route without #

我的模块外观

  @NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    CoreModule,
    AppRoutingModule,
    AuthModule
  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpInterceptorService,
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})

推荐答案

页面刷新显示未找到404 的原因是,所有Angular路由都应通过index.html文件提供.

The reason why 404 Not Found is showing on page refresh is that all Angular routes should be served via the index.html file.

您可以通过添加具有以下内容的.htaccess文件(在index.html所在的目录中)来解决此问题.

You can fix this issue by adding a .htaccess file (in the same directory where the index.html resides) with the following contents.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.html [L]
</IfModule>

点击下面的链接以获取有关将生产版本部署到Apache 的更多详细信息:

Follow below link for further details regarding deploying production build to Apache:

https://github.com/mgechev/angular-seed/wiki/Deploying-prod-build-to-Apache-2

这篇关于Angular 6构建,刷新页面时未找到错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆