angular - Angular 6 Material:mat-tab-link 通过下划线栏选择
问题描述
我的网站有一个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>
推荐阅读
- javascript - 在 ScalaJS 中将 DOM 元素传递给第 3 方类构造函数失败
- php - 如何使更新端点更通用?(PHP)
- javascript - 锚链接在桌面上无法正常工作 - 在移动设备上工作正常
- java - 用户身份验证失败时的 Spring Security ProviderNotFoundException
- powershell - Powershell AD 语法 - 如果语句正在复制字符串
- php - 包含单引号和双引号的回显字符串以输入值
- textfield - Flutter Web - 文本字段滚动而不是选择长文本
- python - 如何从特定时间开始播放音频文件
- flutter - How can I wrap the height of the ListView.builder with a Container?
- html - R:是否可以将“html 文件”插入 MS Word 文档/MS Powerpoint(Plotly)