Html 网址中没有散列的Angular2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41662326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2 without hash in the url
提问by Emilio Weba
Now, my website's url looks like this because I'm using the approach described here
现在,我网站的 url 看起来像这样,因为我正在使用这里描述的方法
http://localhost:4200/#/cadastro
http://localhost:4200/#/cadastro
Is it possible to remove the hash in the url and not get the 404 error?
是否可以删除 url 中的哈希值而不出现 404 错误?
EDIT: Router Module added
编辑:添加路由器模块
const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'cadastro', component: CadastroNoivosComponent },
{ path: '**', component: HomeComponent }
];
export const routing = RouterModule.forRoot(appRoutes);
采纳答案by duke636
If you use PathLocationStrategyas describe hereyou can remove the hash in the URL.
如果您按照此处的描述使用PathLocationStrategy,则可以删除 URL 中的哈希值。
But getting rid of 404 error needs some server side tweak. A quick and easy way is to configure your server to load the home page when any URL of the form http://yourhost/*
is requested.
但是摆脱 404 错误需要一些服务器端调整。一种快速简便的方法是配置您的服务器以在http://yourhost/*
请求表单的任何 URL 时加载主页。
回答by AJT82
If you are using Angular final, the reasons to the hash could be:
如果您使用的是 Angular final,则散列的原因可能是:
RouterModule.forRoot(yourRoutesHere, { useHash: true })
So by removing that could help.
因此,通过删除它可能会有所帮助。
RouterModule.forRoot(yourRoutesHere)
Alternatively if you in your providers (in NgModule) have used:
或者,如果您在提供程序中(在 NgModule 中)使用过:
{provide: LocationStrategy, useClass: HashLocationStrategy}
just remove that.
只需删除它。
EDIT, if you need LocationStrategy, try changing HashLocationStrategy
to PathLocationStrategy
:
编辑,如果您需要 LocationStrategy,请尝试更改HashLocationStrategy
为PathLocationStrategy
:
{provide: LocationStrategy, useClass: PathLocationStrategy}
More about LocationStrategy here
更多关于 LocationStrategy在这里
Now that I have seen your routes as well regarding your 404 issue, you could try changing the following
既然我已经看到了关于 404 问题的路线,您可以尝试更改以下内容
{ path: '**', component: HomeComponent }
to:
到:
{ path: '**', redirectTo: '', pathMatch: 'full' }
More about routing here
更多关于这里的路由
Also check that in your index.html
you have set the basehref like so:
还要检查您index.html
是否像这样设置了 basehref:
<base href="/">