代码之家  ›  专栏  ›  技术社区  ›  Marcus Edensky

角度6材质:材质选项卡链接由下划线栏选择

  •  18
  • Marcus Edensky  · 技术社区  · 6 年前

    我有一个 mat-tab-nav-bar 我的网站的导航栏,但 mat-tab-link 蓝色下划线栏不会追踪活动按钮。它只停留在第一个按钮上,不动。这些按钮确实会变为活动状态,尽管背景颜色会发生变化,它们会很好地传送到相应的页面。

    这是 app.component.ts :

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    
    export class AppComponent {
      navLinks = [
        { path: '', label: 'The Problem' },
        { path: 'the-solution', label: 'The Solution' },
        { path: 'the-game', label: 'The Game' },
        { path: 'probability-calculator', label: 'Probability calculator' },
      ];
    }
    

    下面是 app.component.html :

    <nav mat-tab-nav-bar>
      <a mat-tab-link
         *ngFor="let link of navLinks"
         [routerLink]="link.path"
         routerLinkActive #rla="routerLinkActive"
         [active]="rla.isActive">
        {{link.label}}
      </a>
    </nav>
    
    <router-outlet></router-outlet>
    

    这是 app.module.ts :

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { MatTabsModule } from '@angular/material/tabs';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    import { AppRoutingModule } from './app-routing.module';
    
    import { AppComponent } from './app.component';
    import { TheProblemComponent } from './the-problem/the-problem.component';
    import { TheSolutionComponent } from './the-solution/the-solution.component';
    import { ProbabilityCalculatorComponent } from './probability-calculator/probability-calculator.component';
    import { TheGameComponent } from './the-game/the-game.component';
    
    @NgModule({
      declarations: [
        AppComponent,
        TheProblemComponent,
        TheSolutionComponent,
        ProbabilityCalculatorComponent,
        TheGameComponent
      ],
      imports: [
        AppRoutingModule,
        BrowserModule,
        BrowserAnimationsModule,
        MatTabsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    我错过了什么?非常感谢。

    编辑

    我编辑了 应用程序。组成部分html 如需了解有关链接“活动”状态的更多信息,请执行以下操作:

    <nav mat-tab-nav-bar>
      <a mat-tab-link
         *ngFor="let link of navLinks"
         [routerLink]="link.path"
         routerLinkActive #rla="routerLinkActive"
         [active]="rla.isActive">
        {{link.label}}
        <div style="color: red; margin-left: 10px;">
            <span *ngIf="rla.isActive"> Is active!</span>
            <span *ngIf="!rla.isActive"> Is not active...</span>
        </div>
      </a>
    </nav>
    
    <router-outlet></router-outlet>
    

    事实证明,菜单中的第一个链接始终保持活动状态( rla.isActive )-当我导航到其他页面时也是如此。所有其他链接都会很好地关闭其活动状态,并且只有在导航到它们时才会被激活。导航到其他链接时,如何关闭第一个链接的活动状态?

    编辑2

    正在添加 app-routing.module.ts 代码:

    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    
    import { TheProblemComponent } from './the-problem/the-problem.component';
    import { TheSolutionComponent } from './the-solution/the-solution.component';
    import { TheGameComponent } from './the-game/the-game.component';
    import { ProbabilityCalculatorComponent } from './probability-calculator/probability-calculator.component';
    
    const routes: Routes = [
        { path: '', component: TheProblemComponent },
        { path: 'the-solution', component: TheSolutionComponent },
        { path: 'the-game', component: TheGameComponent },
        { path: 'probability-calculator', component: ProbabilityCalculatorComponent }
    ];
    
    
    @NgModule({
      imports: [ RouterModule.forRoot(routes) ],
      exports: [ RouterModule ]
    })
    export class AppRoutingModule { }
    
    5 回复  |  直到 5 年前
        1
  •  29
  •   Maciej Sikorski    6 年前

    它不适用于您,因为每个都有相同的#rla变量

    您可以这样做:

    <nav mat-tab-nav-bar>
      <a mat-tab-link
         *ngFor="let link of navLinks"
         [routerLink]="link.path"
         routerLinkActive #rla="routerLinkActive"
         [active]="link.isActive">
        {{link.label}}
      </a>
    </nav>
    
    <router-outlet></router-outlet>

    或使用{精确:真}

    <nav mat-tab-nav-bar>
      <a mat-tab-link
         *ngFor="let link of navLinks"
         [routerLink]="link.path"
         routerLinkActive #rla="routerLinkActive"
         [routerLinkActiveOptions]="{exact:true}"
         [active]="rla.isActive">
        {{link.label}}
      </a>
    </nav>
    
    <router-outlet></router-outlet>
        2
  •  11
  •   suhailvs    5 年前

    使用不同的局部变量( #rla1 ,则, #rla2 )为我工作,因为我没有使用 *ngFor :

    <nav mat-tab-nav-bar>
        <a mat-tab-link
           [routerLink]="['/home/homepage/dashboard/']"
           routerLinkActive #rla1="routerLinkActive"
           [active]="rla1.isActive">Dashboard
        </a>
        <a mat-tab-link
           [routerLink]="['/home/homepage/reports/']"
           routerLinkActive #rla2="routerLinkActive"
           [active]="rla2.isActive">Reports
        </a>
    </nav>
    
        3
  •  8
  •   Edric Prince Bansal    6 年前

    你错过了一个 / 链接之前:

    <nav mat-tab-nav-bar>
      <a mat-tab-link
         *ngFor="let link of navLinks"
         [routerLink]="['/'+link.path]"
         routerLinkActive #rla="routerLinkActive"
         [active]="rla.isActive">
        {{link.label}}
      </a>
    </nav>
    
    <router-outlet></router-outlet>
    

    编辑:正确的方法是为所有管线设置非空路径。然后可以将通配符与重定向一起使用。

    const APP_ROUTES: Route[] = [
      { path: 'path-1', component: OneComponent },
      { path: 'path-2', component: TwoComponent },
      { path: 'path-3', component: ThreeComponent },
      { path: '**', redirectTo: 'path-1' },
    ]
    
        4
  •  5
  •   Paul Roub jim    6 年前

    您需要添加 [routerLinkActiveOptions]="{exact:true}" a 标签 它变成

    <nav mat-tab-nav-bar>
      <a mat-tab-link
         *ngFor="let link of navLinks"
         [routerLink]="link.path"
         routerLinkActive #rla="routerLinkActive"
         [routerLinkActiveOptions]="{exact:true}"
         [active]="rla.isActive">
        {{link.label}}
        <div style="color: red; margin-left: 10px;">
            <span *ngIf="rla.isActive"> Is active!</span>
            <span *ngIf="!rla.isActive"> Is not active...</span>
        </div>
      </a>
    </nav>
    
    <router-outlet></router-outlet>
    
        5
  •  0
  •   Prasanna    6 年前

    您只需遵循以下代码即可。 active 是简单的引导类。

    <nav mat-tab-nav-bar>
      <a mat-tab-link
      *ngFor="let link of navLinks"
      [routerLink]="['/'+link.path]"
      routerLinkActive="active">
      {{link.label}}
      </a>
    </nav>
    

    或者你可以这样做,而不是使用下面的材料设计。请注意 routerLinkActiveOptions 已使用。

    <ul class="nav nav-tabs">
        <li role="presentation"
            routerLinkActive="active"
            [routerLinkActiveOptions]="{exact: true}">
          <a routerLink="/">The Problem</a>
        </li>
        <li role="presentation"
            routerLinkActive="active">
          <a routerLink="/the-solution">The Solution</a>
        </li>
        <li role="presentation"
            routerLinkActive="active">
          <a [routerLink]="/the-game">The Game</a>
        </li>
        ....
        ....
     </ul>