部署到Springboot应用程序后,角路由不起作用 [英] Angular routing doesn't work after deploy into a Springboot Application
问题描述
我已经构建了一个包含Angluar5的Springboot应用程序.我有一个gradle构建脚本,将角度文件加载到springboot项目中.这些文件位于springboot项目的 resources/static 下.当我启动我的应用程序时,角度路由不再起作用,我得到
I have build a Springboot Application including Angluar5. I have a gradle build script which loads the angular files into my springboot project.The files are under resources/static of my springboot project. When i start my application the routing of angular is not working anymore and i get
错误:无法匹配任何路由.网址段:访问"
Error: Cannot match any routes. URL Segment: 'access'
我的项目结构:
我使用以下语句部署了我的角度应用程序:
I deployed my angular app with following statement:
ng build --deploy-url = BeatAcknowledgeTest --op = ../backend/src/main/resources/static
ng build --deploy-url=BeatAcknowledgeTest --op=../backend/src/main/resources/static
这将使我的静态文件可访问以下链接:
This will make my static files accessable to following link:
www.mySite.com/BeatAcknowledgeTest/...
www.mySite.com/BeatAcknowledgeTest/...
如果我输入
www.mySite.com/BeatAcknowledgeTest/access
www.mySite.com/BeatAcknowledgeTest/access
页面呈现出来,一切都很好,但是当我在另一个组件中时,例如
the pages renders and all is good but when i am in another component, for example
www.mySite.com/BeatAcknowledgeTest/home
www.mySite.com/BeatAcknowledgeTest/home
然后我点击将我转至
www.mySite.com/BeatAcknowledgeTest/access
www.mySite.com/BeatAcknowledgeTest/access
我遇到错误,我的应用程序未重定向.
i am getting an error and my application is not redirecting.
有什么建议吗?
推荐答案
我通过在我的应用中创建RequestForwardingController解决了此问题.它将处理所有路由:
I solved this issue by creating RequestForwardingController in my app. Which will take care of all the routing:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RequestForwardingController {
@RequestMapping(value = "/**/{[path:[^\\.]*}")
public String redirect() {
// Forward to home page so that angular routing is preserved.
return "forward:/";
}
}
它也解决了重新加载URL的问题.在其中单击浏览器的重新加载按钮,它将显示Spring boot error页面.
It also solves the reload URL issue as well. In which you click on reload button of browser it will show Spring boot error page.
这篇关于部署到Springboot应用程序后,角路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!