gpt4 book ai didi

Angular 6 - 如何在单击其子菜单项时使用 routerLinkActive 将父菜单项设置为事件状态?

转载 作者:行者123 更新时间:2023-12-05 08:23:02 25 4
gpt4 key购买 nike

我使用的是 Angular 6,但我遇到了有关我的导航菜单的问题。

当我点击一个子项时,使用 routerLinkActive 时只有子项被设置为事件,而不是父项。

我想知道在 Angular 6 中选择子项目时如何将父项目类设置为事件状态?

假设我单击了服务的子项 Floor Maintenance。只有 Floor Maintenance 将被设置为事件,但 Services 不会。我希望在单击其任何子项时使用 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 { }

最佳答案

所以当我将我的代码与 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>

关于Angular 6 - 如何在单击其子菜单项时使用 routerLinkActive 将父菜单项设置为事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52884264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com