首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >角度6生成,刷新页时找不到错误404。

角度6生成,刷新页时找不到错误404。
EN

Stack Overflow用户
提问于 2018-08-06 11:52:48
回答 5查看 14K关注 0票数 5

在刷新页面时,我遇到了404页在生产构建中找不到的问题。使用#路由RouterModule.forRoot(routes, { useHash: true })构建良好的工作状态。但我想要一条没有#的漂亮路线

我的模块看起来就像

代码语言:javascript
复制
  @NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    CoreModule,
    AppRoutingModule,
    AuthModule
  ],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpInterceptorService,
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
EN

回答 5

Stack Overflow用户

发布于 2018-08-06 12:09:46

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

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

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

票数 13
EN

Stack Overflow用户

发布于 2018-11-03 09:30:56

在您的.htaccess位置创建index.html 文件

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

注意:RewriteBase/示例/示例是您的index.html基url

<base href="/example/example/">

票数 6
EN

Stack Overflow用户

发布于 2019-09-02 09:46:41

Apache

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

代码语言:javascript
复制
try_files $uri $uri/ /index.html;

用于IIS

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

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

代码语言:javascript
复制
"rewrites": [ {
  "source": "**",
  "destination": "/index.html"
} ]

来源

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51707010

复制
相关文章

相似问题

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