【问题标题】:404 ERROR when an angular application deployed on tomcat server is reloaded重新加载部署在 tomcat 服务器上的 Angular 应用程序时出现 404 错误
【发布时间】:2018-06-08 11:52:29
【问题描述】:

我在 tomcat 服务器上部署了一个 Angular 应用程序,应用程序位于 webapp/test/dist 文件夹中。当我运行 tomcat http://localhost:8080/test/dist/ 时,如果我点击一个链接,该页面将在此时打开,它将打开另一个页面。此时如果刷新页面或将 url 复制并粘贴到新选项卡中会导致 404 错误。我需要配置tomcat服务器,以便当找不到请求的文件而不是返回404时,它应该返回位于webapp/test/dist/文件夹中的index.html。我在server.xml中添加并在conf中创建了rewrite.config文件/catalina/localhost/rewrite.config 但我不知道我要写什么请帮助我。

【问题讨论】:

  • 您需要为您的 tomcat 服务器重写 url:stackoverflow.com/questions/46299430/…
  • 当您将 Angular 应用程序部署到 tomcat 时,您需要使用 --base-href 选项构建项目,请按照以下 url 进行参考。 sudhasoftjava.wordpress.com/2018/05/17/…
  • @sudhakar 尽管我没有使用 --base--href 设置路径,但我在生成的 index.html 中手动设置了路径。
  • @Riscie 我写了重写配置,它的内容是: RewriteCond %{REQUEST_URI} ^/* //我们匹配所有条件模式 RewriteRule ^/test/dist/*$ ^/test/dist/ index.html [L]
  • 在使用 querySelector 方法后它开始工作,但这里的 URL 正在改变。除了这些查询选择器方法和 HashLocationStrategy 方法之外,没有其他方法了吗?

标签: angular


【解决方案1】:

在你的 app.module.ts 中导入:

import {HashLocationStrategy, LocationStrategy} from '@angular/common';

在提供者部分添加:

{provide: LocationStrategy,
  useClass: HashLocationStrategy}

和测试。

【讨论】:

    猜你喜欢
    • 2018-05-02
    • 2022-06-20
    • 2018-08-24
    • 2013-02-07
    • 2019-10-01
    • 2016-05-07
    • 2017-06-20
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多