我想将带有 Angular 前端的 spring-boot webapp 部署到 tomact-server。我使用 angular-cli 和以下命令构建 Angular 应用
ng build --deploy-url /mywebapp/
该应用程序可以运行,但仍然存在以下问题:
- 每次我必须手动将基本 href 标签更改为
<base href="/mywebapp/">
.为什么这不是在构建过程中自动完成的? - “assets”目录的所有静态资源路径也必须手动更改为“/mywebapp/assets”(即
<img src="/mywebapp/assets/logo.png">
) - 路由器链接 URL(子路径)的浏览器刷新不再起作用。 (即,到 http://localhost:8080/mywebapp/feature1 的 Angular 导航有效,但如果我想在浏览器中刷新相同的 URL,则它不起作用。我只能刷新根 URL http://localhost:8080/mywebapp/)
最佳答案
无论何时构建
构建它
ng build --base-href .
这将解决您的问题#1 和#2
对于问题 3
这个 SO 帖子会起作用 angular html5mode in tomcat apache 8.5.4 gives 404 on page refresh
- 加入/opt/tomcat/conf/context.xml
<!-- REWRITE VALVE -->
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
<!-- // -->
- 在特定项目的 WEB-INF 文件夹中添加 rewite.config 文件,内容如下
例如。在/opt/tomcat/webapps/projectName/WEB-INF/rewrite.config
RewriteCond %{REQUEST_URI} !^.*\.(bmp|css|gif|htc|html?|ico|jpe?g|js|pdf|png|swf|txt|xml|svg|eot|woff|woff2|ttf|map)$ RewriteRule ^(.*)$ /index.html [L]
这是做什么的 对于每个此路由请求,它都重定向到 index.html
关于angular - Angular deploy-url 开关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44132284/