我在我的项目 prj-routing-final 中生成了 dist 文件夹,使用 angular-cli 和命令 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 中可用。重新加载后出现相同类型的错误
找不到网址为:http://localhost:8080/recipes/0 的网页
GitHub repo of my project
请帮助我。
最佳答案
您需要配置您的 Web 服务器以将所有未知 (404) 请求重定向到 index.html,这将允许 Angular 应用程序将您的用户路由到所需的路由/ View 。
关于将 dist 文件夹部署到 tomcat 服务器时,angular2 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44832705/