Angular - Apply style to element depending on sibling RouterLinkActive?

前端 未结 5 1883
栀梦
栀梦 2021-02-20 15:44

I do not have only one menu bar on my app that I need to be painted when the user navigates, I have another components too that needs to be painted as well. Can I achieve this j

5条回答
  •  野性不改
    2021-02-20 16:32

    You can call [class.active]="router.isActive('/parentUrlFragment',false)" on Parent Selector to have active class on the parent.

    On your TS file :

    import { Router, ActivatedRoute } from '@angular/router';
    constructor(public router: Router){}
    

    Now you can access the isActive method of Router in your html;

    So if you have a Nested Menu Like :

    Product
    > Category 1
    > Category 2
    

    Selecting any of the Category Link will have active class on Product selector with [class.active]="router.isActive('/product',false)"

提交回复
热议问题