我在导航栏上嵌套了导航栏,单击导航栏内的第一个按钮,它转到doctors/doctors-list,并且路由器链接活动正常。但是当我转到那些路由时,我还有其他子路由器,例如doctors/creatdoctors/update:id,所以我的路由器链接未按预期激活。我的应用程序结构是用户第一次单击“医生”按钮时的doctors/doctors-list
当用户从列表路径中选择医生时,将更改为“ doctors / update:id”。如何解决此问题

<a  routerLink="doctors/doctors-list" routerLinkActive="active"></a>


我的路线配置

{
        path: 'doctors',
        component: DoctorsHomeComponent,
        children: [
        {
            path: 'doctors-list',
            component: DoctorsComponent,
        },
        {
            path: 'doctors/creat',
            component: DoctorsPageComponent,
        },
        {
            path: 'update-doctor/:id',
            component: DoctorsPageComponent,
        }

        ]

    }

最佳答案

你有试过吗

@Component

import { Router } from '@angular/router';``

constructor(public router: Router) {}

isdoctorsActive(exact) {
    return this.router.isActive('doctors/', exact);
}

@html
<a  routerLink="doctors/doctors-list" [ngClass]="{'active': isdoctorsActive(false)}"></a>

10-06 03:53