我已经在 Tomcat 下发布了一个 Angular 应用程序并且它工作正常但是一旦用户刷新页面 Tomcat 回答他找不到 URL(以前使用 Angular 路由处理),另一方面使用 localhost ' ng serve' 它工作正常。
知道如何解决吗?
谢谢
最佳答案
如果服务器上没有重定向规则,应用程序深层链接将无法工作。所有深层链接都必须由服务器重定向到应用程序 index.html
。
设置 tomcat 以重定向任何深层链接 -
- 在 server.xml 中配置 RewriteValve
- 在rewrite.config中写入重写规则
<强>1。在 server.xml 中配置 RewriteValve
编辑 ~/conf/server.xml 以在 Host 部分添加以下 Valve,如下所示 –
...
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
...
</Host>
...
<强>2。在rewrite.config中写入重写规则
创建目录结构 - ~/conf/Catalina/localhost/并在其中使用以下内容创建 rewrite.config 文件。注意 - 这里我将 /hello
视为应用程序的上下文路径。
RewriteCond %{REQUEST_PATH} !-f
RewriteRule ^/hello/(.*) /hello/index.html
设置完成后重新启动 tomcat 服务器,您可以点击应用程序的深层链接,该链接将路由到 Angular 应用程序内的正确组件。
可以引用this post了解更多详情。
关于Angular 和 Tomcat 网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52723841/