gpt4 book ai didi

Angular 2 路由器链接激活,两个链接激活

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

我有一个具有以下路线的项目:

{ 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”和“/cadadstrar/jogos”时,想要访问这两条路由时相同的routerLinkActive处于事件状态。

标题.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>

最佳答案

  • 如果您希望您的路线为 激活一条路线

  • 添加 [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

    注意两点:
  • 在 app-routing.module.ts 中你会看到有两条相似的路由:
  • { path: 'heroes/:id', component: HeroDetailComponent },
    { path: 'heroes', component: HeroesComponent }
  • 在 app.component.ts(在嵌入的模板代码中)你会看到它们是一个路由:
  • <routerLink="/heroes" routerLinkActive="active">Heroes</a>
    此路由匹配 app-routing.module.ts 中设置的两个路径 - 这意味着它将为任何以 开头的路由应用事件类。/英雄

    更新:

    在您的示例中,您可以通过将路由更改为以下内容来执行此操作:

    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);`

    头文件.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>

    关于Angular 2 路由器链接激活,两个链接激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250271/

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