角度:在产品模式下刷新后,URL会发生变化

时间:2018-10-17 01:12:56

标签: angular

我已经构建了一个角度应用程序并将其部署到HTTP服务器。以下是我请求index.html页的方式。

http://localhost:8080/contextRoot/index.html

页面加载且URL更改为http://localhost:8080/contextRoot/XYZ

当我刷新此页面时,它显示错误404,未找到。

我如何要求angular不将URL更改为http://localhost:8080/contextRoot/XYZ,而是使用类似http://localhost:8080/contextRoot/index.html#Login的类似AngularJS的方法

2 个答案:

答案 0 :(得分:1)

你没有。

Angular应用是单页应用(SPA)。

单个页面称为index.html

无论路由/ URL是什么,您始终投放index.html

Angular-CLI开发服务器AKA Webpack开发服务器对此进行了现成的配置。

将Angular App部署到其他Web服务器时,您需要自行配置。

只有Google“ Angular Single Page App的NGINX配置”或 “单页应用程序的Apache配置”

然后相应地配置您的Web服务器。

PS刷新时得到404的原因是因为您的Web服务器的默认行为是查找/contextRoot/XYZ/index.html,这显然不存在。您需要如上所述覆盖此默认行为。

答案 1 :(得分:0)

您是否在nginx上运行?如果您要在Nginx中运行生产版本,请确保将此代码放入配置文件中

location / {
    try_files $uri $uri/ /index.html; # This will allow you to refresh page in your angular app. Which will not give error 404.
}