访问组件类中的[routerLinkActive]值

Pla*_*tus 5 javascript typescript angular

我有一个标签组件TabComponent,它具有以下HTML模板:

<a [routerLink]='link' [routerLinkActive]="[is-active]">link label</a>
<button>Close tab</button>
Run Code Online (Sandbox Code Playgroud)

我想访问[routerLinkActive]属性的值,基本上我想在组件类中获取一个变量,指示此routerLink是否处于活动状态.如何从组件类中访问它?

编辑:我想如果我可以访问<a>链接标记的CSS类,工作完成,有没有办法访问它?

Nay*_*fin 6

我不知道这是否会有所帮助,但您可以在模板中获取如下值:

<a *ngFor="let child of directory; let i = index;"
   routerLink="{{child.route}}"
   routerLinkActive #rla="routerLinkActive">

   <h3>{{rla.isActive}}</h3><!--Displays boolean indicating whether or not you are navigated to this link-->
</a>
Run Code Online (Sandbox Code Playgroud)

现在,您的h3中将显示一个布尔值,指示您当前是否已导航到该链接.

虽然这可以作为模板变量,但我无法将值传递给component.ts或将其传递给其他模板指令,例如

 <a *ngFor="let child of directory; let i = index;"
   routerLink="{{child.route}}"
   routerLinkActive #rla="routerLinkActive"

   [active]="rla.isActive" <!--This doesn't insert the boolean here for some reason-->
   >    
   <h3>{{rla.isActive}}</h3>
</a>
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.如果您接近一个好的解决方案,请更新.