在Angular 7中删除特定路线的HashTag

时间:2019-11-28 09:35:42

标签: angular angular-routing

路由模块具有通用的标签标记{ useHash: true },没有特定的路径。

  { path: 'documentDelivered', component: DocumentDeliveredComponent },
  { path: 'health', component: MonitoringComponent },

  @NgModule({
  imports: [RouterModule.forRoot(routes, { useHash: true })],

http://localhost:4200/#/documentDelivered

  

我只需要删除“健康”路径下的主题标签(#)。

http://localhost:4200/#/health

1 个答案:

答案 0 :(得分:0)

我执行的步骤很正常

  1. 从路由器模块中删除useHash: true,默认情况下为false。 enter image description here

  2. 在索引页面的基础href中添加/斜杠

enter image description here

现在它可以在开发环境中正常工作了。

对于产品,我们需要在(.htaccess)文件中添加规则。

修复了将重定向限制到路由器中的同级和子级路径的问题。 基本上,问题出在产品中,而不是在开发中。

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Redirection of requests to index.html
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
  RewriteRule ^.*$ - [NC,L]
  RewriteRule ^(.*) index.html [NC,L]
</IfModule>

参考:https://julianpoemp.github.io/ngx-htaccess-generator/#/generator

欢呼:)