【问题标题】:Angular Router usehash: navigate is not preserve the url before the hastagAngular Router usehash:导航不保留主题标签之前的url
【发布时间】:2020-09-08 15:53:37
【问题描述】:

我有以下网址:

https://localhost:4200/example/#/

当我没有像以下示例所示那样导航其他角度路线时,/example 将从 URL 中删除。生成的 URL 是 https://localhost:4200/#/firstSegment/secondSegment 而不是 https://localhost:4200/example/#/firstSegment/secondSegment

...
constructor(private router: Router) {}
....

navigate() {
    this.router.navigate([`/firstSegment/secondSegment`]);
}

有一种方法可以在每次执行导航时保留# 之前的 URL 部分?

遵循我的路由器配置:

@NgModule({
   .....
   imports: [
      .....
      RouterModule.forRoot(
      appRoutes,
      {
        enableTracing: false, // <-- debugging purposes only
        useHash: true,
        initialNavigation: false
      }
     .....
   ]

})

【问题讨论】:

  • 哪个角版本?
  • @RafaelPizao Angular 8.
  • 从 Angular 6.1 开始,可以使用片段滚动页面。您使用页面滚动还是“第二段”是一条新路线?
  • @RafaelPizao 我想一直维护的第一部分。第二部分表示 appRoutes 中定义的角度死记硬背,它将加载给定的角度组件。第一部分是对运行应用程序很重要的信息。
  • 在 index.html 中设置

标签: javascript angular typescript angular-router


【解决方案1】:

对于默认路由,需要设置前缀为angular.json

...
"projects": {
    "app-name": {
      "prefix": "example",
    ...
}
...

更多细节 https://angular.io/guide/workspace-config

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 2023-03-14
    相关资源
    最近更新 更多