In Angular 2 how do I assign a custom class to an active router link? [duplicate]

允我心安 提交于 2019-11-28 11:37:58

AFAIK this isn't supported directly. As a workaround you could add a directive that checks if the class router-link-active is set and depending on that add/remove your custom class.

@Directive({
  selector: '[routerLink]')
export class RouterLinkReplaceClass {
  // add class `my-active` when `myActiveClass` is `true`
  @HostBinding('class.my-active') 

  // read `router-link-active` class state
  @Input('class.router-link-active') 

  myActiveClass: bool = false;
}

Plunker example

To use it just add it to the directives of the parent component. (not tested)

I found something to handle this in an AngularClass project. Check out the custom RouterDirective. Seems to be a way to to get this handled, but I haven't put it to my project yet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!