我正在使用Angular 6,但在导航菜单方面遇到了问题。
当我单击子项时,使用routerLinkActive时,只有子项设置为活动状态,而不是父项。
我想知道如何在Angular 6中选择其父项时将其设置为活动状态?
假设我单击“服务”的子项“地板维护”。仅“地板维护”将设置为活动状态,而不是“服务”。我想在单击其任何子项时使用routerLinkActive激活“服务”按钮。
<nav class="navbar navbar-expand-lg navbar-light bg-white thick-border-bottom">
<div class="container">
<a class="navbar-brand" routerLink=""><img id="logo" class="img-fluid" src="./assets/logo.svg" alt=""></a>
<button class="navbar-toggler my-3" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" routerLink="" routerLinkActive="active" [routerLinkActiveOptions]={exact:true}>Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/about" routerLinkActive="active">About</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" routerLink="services" routerLinkActive="active" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">
Services
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" routerLink="services/janitorial-services" routerLinkActive="active">Janitorial Services</a>
<a class="dropdown-item" routerLink="services/floor-maintenance" routerLinkActive="active">Floor Maintenance</a>
<a class="dropdown-item" routerLink="services/green-cleaning-services" routerLinkActive="active">Green Cleaning</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/our-difference" routerLinkActive="active">Our Difference</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/our-clients" routerLinkActive="active">Our Clients</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/contact" routerLinkActive="active">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AboutComponent } from './frontend/pages/about/about.component';
import { HomeComponent } from './frontend/pages/home/home.component';
import { ServicesComponent } from './frontend/pages/services/services.component';
import { JanitorialServicesComponent } from './frontend/pages/services/subpages/janitorial-services/janitorial-services.component';
import { ServiceAreasComponent } from './frontend/pages/service-areas/service-areas.component';
import { OurClientsComponent } from './frontend/pages/our-clients/our-clients.component';
import { ContactComponent } from './frontend/pages/contact/contact.component';
import { FloorMaintenanceComponent } from './frontend/pages/services/subpages/floor-maintenance/floor-maintenance.component';
import { GreenCleaningServicesComponent } from './frontend/pages/services/subpages/green-cleaning-services/green-cleaning-services.component';
import { OurDifferenceComponent } from './frontend/pages/our-difference/our-difference.component';
const routes: Routes = [
{ path: '', redirectTo: '/', pathMatch: 'full' },
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'services', component: ServicesComponent,
children: [
{path: "janitorial-services", component: JanitorialServicesComponent},
{ path: "green-cleaning-services", component: GreenCleaningServicesComponent},
{ path: "floor-maintenance", component: FloorMaintenanceComponent}
]
},
{ path: 'our-difference', component: OurDifferenceComponent },
{ path: 'our-clients', component: OurClientsComponent },
{ path: 'contact', component: ContactComponent }
];
@NgModule({
exports: [RouterModule],
imports: [RouterModule.forRoot(routes)],
})
export class AppRoutingModule { }
答案 0 :(得分:4)
因此,当我将代码与https://stackblitz.com/edit/parent-child-active?file=src%2Fapp%2Fapp.component.css进行比较时。
我可以找到的唯一区别是,我将其设置为a标签的routerLinks和routerLinkActive属性。
所以我决定改成这样的结构:
<ul>
<li routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a href="#" routerLink="/">Home</a>
</li>
<li routerLinkActive="active">
<a href="#" routerLink="/parent">Parent Component</a>
<ul>
<li routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a href="#" routerLink="/parent/child">Child Component</a>
</li>
</ul>
</li>
</ul>
答案 1 :(得分:1)
如果您希望父类在子类也保持活动的同时保持活动状态,则需要将子类的routerLink设置为/ parent / child。如果活动的routerLink的开头与另一个完全匹配,则两者都会变为活动状态:
<div>
<a routerLink="/Parent" routerLinkActive="active">Parent</a>
<a routerLink="/Parent/Child" routerLinkActive="active">Child</a>
</div>
答案 2 :(得分:0)
您可以使用模板变量来实现。检查子组件中的#rla
变量。
<li [ngClass]="{ 'is-expanded': rla.isActive }">
<a>Parent</a>
<ul>
<li>
<a routerLink="/child" routerLinkActive="active" #rla="routerLinkActive">Child</a>
</li>
</ul>
</li>