Angular 7当子链接为routerLinkActive时,使父主题活动

时间:2019-02-22 19:24:32

标签: angular

如果子链接为 routerLinkActive ,我试图使h2具有“活动”类。

如果查看:Angular 6 - How do I set a Parent Menu Item To Active using routerLinkActive When Clicking on Its Child Menu Item?

https://angular.io/api/router/RouterLinkActive

但都没有帮助吗?

我的代码:

        <ng-container *ngFor="let item of nav.Navigation.MenuItem; let r = index">
            <div class="navGroup" *hasClaim="item.Claim">
                <h2 class="acc_trigger" [class.active]="isActive" (click)="navSelect(item, $event)" ><span class="fas {{item.icon}}"></span>{{item.Text}}</h2>
                <ng-container *ngIf="item.MenuItem != null">
                    <div class="acc_container" >
                        <ul class="ulNav">
                            <li *ngFor="let nav of item.MenuItem"><a [routerLink]="nav.Url" routerLinkActive="active-link" >{{nav.Text}}</a></li>
                        </ul>
                    </div>
                </ng-container>
            </div>
        </ng-container>

1 个答案:

答案 0 :(得分:2)

根据文档,它说您应该能够将routerLinkActive放在您要查找的链接的任何祖先(包括子菜单链接列表)上,因此只需将routerLinkActive添加到navGroup

<ng-container *ngFor="let item of nav.Navigation.MenuItem; let r = index">
    <div class="navGroup" *hasClaim="item.Claim" routerLinkActive="active">
        <h2 class="acc_trigger" (click)="navSelect(item, $event)" ><span class="fas {{item.icon}}"></span>{{item.Text}}</h2>
        <ng-container *ngIf="item.MenuItem != null">
            <div class="acc_container" >
                <ul class="ulNav">
                    <li *ngFor="let nav of item.MenuItem"><a [routerLink]="nav.Url" routerLinkActive="active-link" >{{nav.Text}}</a></li>
                </ul>
            </div>
        </ng-container>
    </div>
</ng-container>

并使您的h2样式更像

.navGroup.active>h2 { /* some styles for your active h2 */ }

应该使其正常运行。尽管我已经看到很多抱怨routerLinkActive行为异常,而且它们似乎在每个新版本中都在不断更改-您是否尝试过这种方法?