简体   繁体   English

为什么 Angular routerLink(没有前导斜杠)在导航回空路径时被破坏?

[英]Why is Angular routerLink (without leading slash) broken when navigating back to empty path?

Let's take this basic navigation tree:让我们以这个基本的导航树为例:

  • primary基本的
    • secondary次要的
      • deep深的

Look at my Stackblitz project .看看我的Stackblitz 项目

My routes are我的路线是

const routes: Routes = [
  {
    path: "",
    component: PageParentComponent,
    children: [
      {
        path: "",
        component: PageChildPrimaryComponent
      },
      {
        path: "secondary",
        component: PageChildSecondaryComponent
      },
      {
        path: "secondary/deep",
        component: PageDeepComponent
      }
    ]
  }
];

Using <a routerLink=".."> , I can successfully navigate up one level, and using <a routerLink="../.."> two levels.使用<a routerLink=".."> ,我可以成功导航上一层,并使用<a routerLink="../..">两层。 However, when I use double dots to go back to the "primary" page from any deeper page, any routerLink that doesn't start with a slash is messed up;但是,当我使用双点从任何更深的页面返回“主”页面时,任何不以斜杠开头的routerLink都会搞砸; the root part of the URL ( https://angular-ukxb4a.stackblitz.io/ or localhost:4200/ , when run locally) is replaced with %28 . URL 的根部分( https://angular-ukxb4a.stackblitz.io/localhost:4200/ ,在本地运行时)替换为%28 When I use <a routerLink="/"> (slash instead of double dots), everything is fine.当我使用<a routerLink="/"> (斜线而不是双点)时,一切都很好。

Why is that happening?为什么会这样?

The URL you want to go is你要去的网址是

https://angular-ukxb4a.stackblitz.io/secondary https://angular-ukxb4a.stackblitz.io/secondary

NOT不是

https://%28secondary%29/ https://%28secondary%29/

The starting slash will append path to your existing path.起始斜杠将路径附加到您现有的路径。 I hope this will help you.我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM