如何在角度5中的网址中删除#? [英] How to remove # in url in angular 5?
问题描述
实际上,我在项目中使用的是角度5,我需要从网址中删除#.这是我的网址 http://localhost:4200/#/product/add
.在我的域中发布后,它可以正常工作,但是在刷新页面时,由于我的URL中的#,它显示404错误.可以这样做吗?
Actually i am using angular 5 for my project, I need to remove # from my url.
here is my url
http://localhost:4200/#/product/add
.
After published in my domain it works properly but while refresh the page it shows 404 error because of # in my url. Is it possible to do this?
-
需要删除网址中的#
推荐答案
您需要更改 PathLocationStrategy
默认情况下,使用PathLocationStrategy进行测角,这表示您可能自己在定义 HashLocationStrategy .在您的项目中寻找这种代码:
By default angular use the PathLocationStrategy, that said looks like you're probably defining HashLocationStrategy yourself. Look for some sort of this code in your project:
RouterModule.forRoot(routes, { useHash: true })
在这种情况下,只需删除useHash即可:
In this case just remove the useHash like so:
RouterModule.forRoot(routes)
或者也许
[Location, {provide: LocationStrategy, useClass: HashLocationStrategy}]
在这种情况下,将HashLocationStrategy更改为PathLocationStrategy,如下所示:
In this case change HashLocationStrategy to PathLocationStrategy like so:
[Location, {provide: LocationStrategy, useClass: PathLocationStrategy}]
这篇关于如何在角度5中的网址中删除#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!