url中没有哈希的Angular2

新手上路,请多包涵

现在,我网站的网址看起来像这样,因为我使用的是 此处 描述的方法

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);

原文由 Emilio Weba 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 267
2 个回答

如果您按照 此处 所述使用 PathLocationStrategy ,则可以删除 URL 中的散列。

但是摆脱 404 错误需要一些服务器端调整。一种快速简便的方法是将您的服务器配置为在请求 http://yourhost/* 形式的任何 URL 时加载主页。

原文由 duke636 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果你使用的是 Angular final,散列的原因可能是:

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

因此,删除它可能会有所帮助。

 RouterModule.forRoot(yourRoutesHere)

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

 {provide: LocationStrategy, useClass: HashLocationStrategy}

只是删除它。

编辑,如果您需要 LocationStrategy,请尝试将 HashLocationStrategy 更改为 PathLocationStrategy

 {provide: LocationStrategy, useClass: PathLocationStrategy}

更多关于 LocationStrategy 的信息

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

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

到:

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

更多关于 这里 的路由

还要检查你的 index.html 你已经像这样设置了 basehref:

 <base href="/">

原文由 AT82 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题