我在尝试将我的 Angular2 应用程序部署到 tomcat8 时遇到了一个奇怪的问题。
首先是上下文: - 我使用 Angular Cli 来协助我的构建过程。 - 应用程序与服务器在本地完美运行:ng serve - ng build 使我的 dist/dir 包含所有产品资源 - 我创建了一个 gulp 任务来创建 WAR,以便将应用程序正确部署到 Tomcat。 - war 被部署到tomcat webapps directoy
我的应用 URI 是这样的:https:\www.myexample.com\myapp\
主页 uri 运行良好; 尝试使用 Angular2 路由时问题开始了;例如 https:\www.myexample.com\myapp\myroute.
Tomcats 认为这是一个新的 webapp 目录,并没有做任何转发 https:\www.myexample.com\myapp\index.html
到目前为止,我已经看到好几篇帖子都在谈论这种行为;我还没有看到任何解决方案。
有没有人知道我如何克服这个问题以在 tomcat8 上正确运行我的 angular2 应用程序?
感谢支持。
最佳答案
除非目录或文件存在,否则您需要重写 url 以提供 index.html
。
配置 Tomcat 以使用重写阀 https://tomcat.apache.org/tomcat-8.0-doc/rewrite.html
然后将如下内容添加到 rewrite.config
文件中:
# If dir or asset exists go to it
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# Rewrite all other urls to use index.html
RewriteRule ^ /index.html
关于angular - 将 Angular2 应用程序部署到 Tomcat8 - 路由似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41472774/