我有一个包含以下路线的项目:
{ path: '', component: GamesListingComponent },
{ path: 'inicio', component: HomeComponent },
{ path: 'listar',
children: [
{ path: '', component: HomeComponent },
{ path: 'fabricantes', component: ManufacturesListingComponent },
{ path: 'plataformas', component: PlatformsListingComponent },
{ path: 'usuarios', component: UsersListingComponent },
{ path: 'jogos', component: GamesListingComponent },
{ path: '**', component: HomeComponent }
]
},
{ path: 'cadastrar',
children: [
{ path: '', component: HomeComponent },
{ path: 'fabricantes', component: ManufacturesRegisterComponent },
{ path: 'plataformas', component: PlatformsRegisterComponent },
{ path: 'usuarios', component: UsersRegisterComponent },
{ path: 'jogos', component: GamesRegisterComponent },
{ path: '**', component: HomeComponent }
]
},
{ path: '**', component: GamesListingComponent }
我希望routerLinkActive只能通过子路由的名称启用,例如:
访问" / listar / jogos"和" / cadastrar / jogos",想要访问这两个路由,同一个routerLinkActive是活动的。
了header.html
<div class="jumbotron">
<p class="text-center title"><a [routerLink]="['/inicio']">Lightning Games</a></p>
<p class="text-center">A sua loja de jogos desde o seu primeiro dia</p>
</div>
<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav mr-auto">
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/inicio']">Início <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/listar', 'plataformas']">Plataformas</a>
</li>
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/listar', 'fabricantes']">Fabricantes</a>
</li>
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/listar', 'usuarios']">Usuários</a>
</li>
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/listar', 'jogos']">Jogos</a>
</li>
</ul>
</div>
</nav>
答案 0 :(得分:7)
添加[routerLinkActiveOptions]="{exact:true}"
将使活动类仅适用于精确路由匹配。
如果这是您想要的,请按以下步骤更新您的<li>
代码:
<li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
<a class="nav-link" [routerLink]="['/inicio']">Início <span class="sr-only">(current)</span></a>
</li>
查看我从一个官方Angular路由示例中修改过的这个Plunkr代码。 https://plnkr.co/edit/wWyVfxDHbSA8tYcpsNsF?p=preview
注意两件事:
{ path: 'heroes/:id', component: HeroDetailComponent },
{ path: 'heroes', component: HeroesComponent }
<routerLink="/heroes" routerLinkActive="active">Heroes</a>
此路由与app-routing.module.ts中设置的路径的两个匹配 - 这意味着它将为以 / heroes
开头的任何路由应用活动类<强>更新强>
在您的示例中,您可以通过将路由更改为以下内容来执行此操作:
app.routes.ts
const appRoutes: Routes = [
{ path: '', component: GamesListingComponent },
{ path: 'inicio', component: HomeComponent },
{ path: '', component: HomeComponent },
{ path: 'jogos', component: GamesListingComponent },
{ path: 'fabricantes', component: ManufacturesListingComponent },
{ path: 'plataformas', component: PlatformsListingComponent },
{ path: 'usuarios', component: UsersListingComponent },
{ path: 'jogos/cadastrar', component: GamesRegisterComponent },
{ path: 'fabricantes/cadastrar', component: ManufacturesRegisterComponent },
{ path: 'plataformas/cadastrar', component: PlatformsRegisterComponent },
{ path: 'usuarios/cadastrar', component: UsersRegisterComponent },
{ path: '**', component: GamesListingComponent }
];
export const routing = RouterModule.forRoot(appRoutes);`
header.component.html
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/inicio']">Início <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/plataformas']">Plataformas</a>
</li>
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/fabricantes']">Fabricantes</a>
</li>
<li class="nav-item" [routerLinkActive]="['active']">
<a class="nav-link" [routerLink]="['/usuarios']">Usuários</a>
</li>
<li class="nav-item" [routerLinkActive]="['active']" >
<a class="nav-link" [routerLink]="['/jogos']">Jogos</a>
</li>
XXX-listing.component.html
<a [routerLink]="['/jogos/cadastrar']" class="btn btn-primary">
Novo Jogo
</a>
答案 1 :(得分:1)
如果您要激活两个链接的路线,请按以下方式更改您的html文件,
<nav class='navbar navbar-expand navbar-light bg-light'>
<div class='width-100'>
<div class='float-left'>
<ul class='nav nav-pills'>
<li><a class='nav-link' [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}"[routerLink]="['/abc']">Abc</a></li>
<li class="nav-link" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}" [routerLink]="['/def']" >Def
<a routerLink="/ghi"></a>
</li>
</ul>
</div>
</div>
</nav>
像这样在app-module.ts文件中定义路由,
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot([
//, canDeactivate : ['canDeactivateCreateEmployee']},
{ path: 'abc', component: AbcComponent },
{ path: 'def', component: DefComponent },
{ path: 'ghi', component: GhiComponent},
{ path: '', redirectTo: 'AbcComponent', pathMatch: 'full' },
{ path: '**', redirectTo: 'AbcComponent', pathMatch: 'full' }
])
],
这将使单个RouterLinkActive具有两个URL(def,ghi)