当您将 dist 文件夹部署到 tomcat 服务器时,angular2 路由不起作用

angular2 routing not working when you deployed dist folder onto tomcat server

我在项目 prj-routing-final 中使用 angular-cli 生成了 dist 文件夹 使用命令 ng-build --prod --aot 并部署到 tomcat 服务器上。我能够导航到所有路线 http://localhost:8081/dist/ 并在不重新加载的情况下查看它们。一旦我导航到 http://localhost:8081/dist/recipes/0 并重新加载页面,就会出现此错误

HTTP Status 404 – Not Found  
Type Status Report
Message /dist/recipes/0
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/8.5.14

如果我使用 ng serve 服务,我不会收到任何错误。我能够导航到所有路线、重新加载页面、来回移动。

我还尝试使用 Http-server 服务 dist,在 https://www.npmjs.com/package/http-server 中可用,重新加载后出现相同类型的错误 No webpage was found for the web address: http://localhost:8080/recipes/0 GitHub repo of my project

请帮助我。

您需要配置您的网络服务器以将所有未知 (404) 请求重定向到 index.html,这将允许 Angular 应用程序将您的用户路由到所需的 route/view.