Angular 6构建,刷新页面时未找到错误404

时间:2018-08-06 11:52:49

标签: angular angular-routing

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

的漂亮路线

我的模块看起来像

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

4 个答案:

答案 0 :(得分:4)

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

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

<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 的更多详细信息:

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

答案 1 :(得分:1)

  • 列表项
    1. 在index.html位置中创建 .htaccess 文件
    2. <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /example/example/
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.html [L]
      </IfModule>
      

注意:  1.RewriteBase / example / example是您的index.html基本URL  <base href="/example/example/">

答案 2 :(得分:1)

对于 Apache

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

对于 Nginx

try_files $uri $uri/ /index.html;

对于 IIS

<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="/index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

对于 GitHub页面

you can't directly configure the GitHub Pages server, but you can add a 404 page. Copy index.html into 404.html. It will still be served as the 404 response, but the browser will process that page and load the app properly. It's also a good idea to serve from docs/ on master and to create a .nojekyll file

对于 Firebase托管

"rewrites": [ {
  "source": "**",
  "destination": "/index.html"
} ]

Source

答案 3 :(得分:0)

深层url被称为SPA支持,默认情况下节点的http服务器不支持,但是可以通过这种方式启用-

http-server --gzip --proxy http://localhost:8080?