简体   繁体   English

带有重定向路由的 Angular RouterLinkActive

[英]Angular RouterLinkActive with redirected routes

I have found an interesting behavior in the way routerLinkActive when using routes with route params.当使用带有路由参数的路由时,我发现routerLinkActive的方式很有趣。 The behavior only occurs when using a redirect route.该行为仅在使用重定向路由时发生。

Consider an application with these two routes:考虑具有以下两条路线的应用程序:

  { path: 'page2/:id', component: Page2Component },
  { path: 'page2', redirectTo: '/page2/', pathMatch: 'full' },

And, in the same app, a page with these links with routerLinkActive :并且,在同一个应用程序中,带有这些链接的页面带有routerLinkActive

      <a 
        routerLink="/page2" 
        routerLinkActive="active"
        [routerLinkActiveOptions]="{exact: true}">Page 2</a>

      <a routerLink="/page2/foo" routerLinkActive="active">Page 2 / Foo</a>

I would expect the first link (/page2) to be have the active class only when the url is /active and not when the url is /active/foo .我希望第一个链接(/page2)仅在 url 为/active而不是当 url 为/active/foo时才具有active类。 However, when I have the redirect route shown above, the first link never gets the active class applied to it.但是,当我有上面显示的redirect路由时,第一个链接永远不会将active类应用于它。

It does work as expected if, instead of the redirect route, I just have a second route as follows:如果我只有第二条路由而不是重定向路由,它会按预期工作,如下所示:

  { path: 'page2/:id', component: Page2Component },
  { path: 'page2', component: Page2Component },

However, it's a better practice to use the redirect route, because then the component does not get re-loaded when navigating to itself from itself (ie navigating from /page2/foo to /page2/bar ).但是,使用重定向路由是一种更好的做法,因为当从自身导航到自身时(即从/page2/foo导航到/page2/bar ),组件不会重新加载。 So I'd prefer the redirect route, but I can't see how to get the [routerLinkActiveOptions]="{exact: true}" working when using a redirect route.所以我更喜欢重定向路由,但我看不到在使用重定向路由时如何让[routerLinkActiveOptions]="{exact: true}"工作。

Here are examples in stackblitz:以下是 stackblitz 中的示例:

Broken example with redirect route: https://stackblitz.com/edit/angular-ivy-ii1882?file=src/app/app.module.ts重定向路由的损坏示例: https ://stackblitz.com/edit/angular-ivy-ii1882?file=src/app/app.module.ts

Working example with non-redirect route: https://stackblitz.com/edit/angular-ivy-rh1tj1?file=src/app/app.module.ts非重定向路由的工作示例: https ://stackblitz.com/edit/angular-ivy-rh1tj1?file=src/app/app.module.ts

Is there a solution that would allow me to have both a redirect route and a working routerLinkActiveOptions ?有没有一种解决方案可以让我同时拥有重定向路由和工作routerLinkActiveOptions

EDIT: The real answer to this problem is to use query strings instead of route parameters for optional parameters.编辑:这个问题的真正答案是使用查询字符串而不是路由参数作为可选参数。 See below for more info.有关更多信息,请参见下文。

Originally my answer was that the correct way to do this is to pass an empty parameter as part of the routerLink like this:最初我的回答是正确的方法是传递一个空参数作为 routerLink 的一部分,如下所示:

[routerLink]="['page2', '']"

EDIT: But actually, this approach has another side effect.编辑:但实际上,这种方法还有另一个副作用。 routerLinks that are set to simply [routerLink]="['page2', '']" act as if {exact: true} is set to true on routerLinkActiveOptions even without adding routerLinkActiveOptions .设置为[routerLink]="['page2', '']"的 routerLinks 就像在routerLinkActiveOptions {exact: true}设置为 true 一样,即使没有添加routerLinkActiveOptions This is because it is looking specifically for a route with an empty string as a parameter.这是因为它专门寻找带有空字符串作为参数的路由。 The real solution here is to use querystring parameters instead of route parameters if the parameter is not required.这里真正的解决方案是在不需要参数的情况下使用查询字符串参数而不是路由参数。

Side note: It does seem like routerLink="page2" could feasibly work the same way and my initial bug report has been changed to a feature request: Github Issue旁注:看起来routerLink="page2"确实可以以相同的方式工作,并且我最初的错误报告已更改为功能请求: Github 问题

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

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