Angular 6 Material : mat-tab-link be selected by underlining bar

标签 angular typescript angular-material navigationbar

我的网站有一个 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 { }

我错过了什么?谢谢!

编辑

我像这样编辑了 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}}
    <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 { }

最佳答案

它对你不起作用,因为每个人都有相同的#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>

或 {exact:true}

<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>

关于 Angular 6 Material : mat-tab-link be selected by underlining bar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50313246/

相关文章:

Angular 2 错误( 'directives' 在类型 'Component' 中不存在)

typescript - 合并 TypeScript 中对象元组的值类型

css - 将 Angular Material 下拉菜单移至左侧

javascript - 如何在 Angular 6 的组件模板中呈现超链接?

html - angular 4 应用程序不填满屏幕高度

node.js - 使用 Angular 版本 2.4.9、TypeScript 和 Webpack 版本 2.2.1 时,类型 'map' 上不存在属性 'Observable<Response>'

android - 元素阴影不明显

json - 如何在 Angular2 中与其动态交互的 2 个组件中注入(inject)相同的服务

angularjs - 到 child 的 Angular 路由 - 来自不同 parent 的 child

angular-material - 在 Angular Material 中没有输入数据时强制标签 float