将Angular2应用程序部署到Tomcat8-路由似乎不起作用

aor*_*vre 5 tomcat8 angular-cli angular

尝试将Angular2应用程序部署到tomcat8时遇到一个奇怪的问题。

首先,上下文:-我使用Angular Cli来进行构建。-应用程序可以在服务器上本地完美运行:ng serve-ng build使我的dist / dir具有所有产品资源-我创建了一个gulp任务来创建WAR,以便将应用程序正确部署到Tomcat。-将战争部署到tomcat webapps directoy

我的应用URI是这样的:https:\ www.myexample.com \ myapp \

Home uri运作良好;当尝试使用Angular2路由时,问题开始了。例如https:\ www.myexample.com \ myapp \ myroute。

Tomcats认为这是一个新的Webapp目录,并且不进行任何转发https:\ www.myexample.com \ myapp \ index.html

到目前为止,我已经看过几篇有关这种行为的文章。我还没有看到任何解决方案。

有谁知道/如何解决这个问题以在Tomcat8上正确运行我的angular2应用程序?

谢谢支持。

shu*_*son 3

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
Run Code Online (Sandbox Code Playgroud)