我目前正在学习Angluar6,现在有点泡菜。 当前布局如下:
如果我按任意按钮,我将转到他们的页面:
/help
/leaderboards
/maingame
这就是我现在想做的。我通过“帮助”按钮给出一个例子。这也应适用于右边的“排行榜”按钮。
这是我的HTML外观:
<div class="containerBtn">
<div class="helpDiv">
<button id="helpBtn" class="secondary" [routerLink]="['/help']" routerLinkActive="activeBtn">
<i class="material-icons">{{ helpBtnIcon }}</i>
</button>
</div>
<div class="leaderboardDiv">
<button id="leaderboardsBtn" class="secondary" (click)=goToLeaderboards() routerLinkActive="activeBtn">
<i class="material-icons">{{ leaderboardsBtnIcon }}</i>
</button>
</div>
<div class="startDiv">
<button id="startBtn" class="primary" [routerLink]="['/maingame']">
Start
</button>
</div>
</div>
如您所见,最初我将routerLink与routerLinkActive一起使用。现在,我正在测试执行一个方法的click事件。但是,这会忽略routerLinkActive。
这是我的TS文件:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.css']
})
export class NavigationComponent implements OnInit {
currentUrl: string;
helpBtnIcon = 'help';
leaderboardsBtnIcon = 'view_headline';
cancelBtnIcon = 'cancel';
constructor(private router: Router) {
this.router.events.subscribe(value => {
this.currentUrl = router.url.toString();
if (this.currentUrl === '/help') {
this.helpBtnIcon = this.cancelBtnIcon;
} else {
this.helpBtnIcon = 'help';
}
});
}
ngOnInit() {
}
goToLeaderboards() {
this.router.navigate(['/leaderboards');
this.leaderboardsBtnIcon = this.cancelBtnIcon;
}
}
所以我对我想做的正确方法有些困惑。 一个问题是我将ICON更改为该“取消”图标,但是,如果我转到另一个页面,则我的按钮仍为X,并且不会切换回原始样式和图标及链接。
谢谢你们的帮助!