【发布时间】:2018-07-23 21:12:55
【问题描述】:
我正在本地开发机器上使用 ng serve 运行 Angular 6 项目。例如,我有以下端点:
- /登录
- /购物车
当我的项目在 localhost:4200 上运行时,我可以输入以下任何一个,它们都是有效的(浏览器会成功加载相应的页面):
- 本地主机:4200
- localhost:4200/登录
- 本地主机:4200/购物车
但是,当我构建 dist 文件夹并将内容推送到我的生产服务器时,我尝试通过相同的 url 访问该站点,但它不起作用:
- http://xxx.xxx.xx.xx -- 有效
- http://xxx.xxx.xx.xx/login -- 不起作用
- http://xxx.xxx.xx.xx/cart -- 不起作用
那些不工作的响应一个 404 not found 错误。在控制台中它说:
加载资源失败:服务器响应状态为 404(未找到)
本地开发服务器和生产服务器上的行为有何不同?如何允许用户直接路由到特定端点,例如 http://xxx.xxx.xx.xx/cart?
这也将解决我的其他问题。我添加了一个功能,如果用户实际上输入了错误的 url,他们将收到 404 错误(如预期的那样)。我在我的 app-routing.module.ts 文件中配置了 404 错误将被重定向到自定义组件。但是,这不再有效。
这是我按项目构建的方式:
ng build --configuration=staging # --configuration=staging specifies that the build should use the staging environment listed in environments folder
文件存储在服务器上的 /var/www/html 文件夹中,并且正在运行 apache2。
【问题讨论】:
标签: angular url-routing angular-routing