Angular2在url中不包含哈希值

问题描述 投票:11回答:3

现在,我的网站的url看起来是这样的,因为我使用了所述的方法。此处

http:/localhost:4200#cadastro

是否可以把url中的哈希值去掉,就不会出现404错误?

编辑:添加了路由器模块

const appRoutes: Routes = [
    { path: '', component: HomeComponent },
    { path: 'cadastro', component: CadastroNoivosComponent },
    { path: '**', component: HomeComponent }
];

export const routing = RouterModule.forRoot(appRoutes);
html angular angular2-routing
3个回答
9
投票

如果你使用 PathLocationStrategy 所谓 此处 你可以删除URL中的哈希值。

但是摆脱404错误需要一些服务器端的调整。一个快速和简单的方法是配置你的服务器,以加载主页,当任何形式的URL http://yourhost/* 被请求。


34
投票

如果你使用的是Angular final,对哈希的原因可能是。

RouterModule.forRoot(yourRoutesHere, { useHash: true })

所以通过删除这个可以帮助你。

RouterModule.forRoot(yourRoutesHere)

或者,如果你在你的提供者(在NgModule中)使用了。

{provide: LocationStrategy, useClass: HashLocationStrategy}

就把它去掉。

EDIT,如果你需要LocationStrategy,可以尝试更改以下内容 HashLocationStrategyPathLocationStrategy:

{provide: LocationStrategy, useClass: PathLocationStrategy}

更多关于LocationStrategy的信息 此处

现在我已经看到了你的路由以及关于你的404问题,你可以尝试更改以下内容。

{ path: '**', component: HomeComponent }

到。

{ path: '**', redirectTo: '', pathMatch: 'full' }

更多关于路由 此处

还可以在你的 index.html 你的basehref是这样设置的。

<base href="/">

1
投票

创建一个.htaccess文件 粘贴以下代码并上传到你的prod服务器上。

RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
© www.soinside.com 2019 - 2024. All rights reserved.