我正在尝试获取 Angular 2 中的活动链接,以便我可以在我的模板中更新我的 css。
这是我在谷歌的帮助下所做的:
export class AppComponent {
router: Router;
constructor(data: Router) {
this.router = data;
}
isActive(tab): boolean {
if (this.router.currentInstruction && this.router.currentInstruction.component.routeData) {
return tab == this.router.currentInstruction.component.routeData.data['activeTab'];
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我的模板如下:
<li class="some-class" [ngClass]="{active: isActive('some-route')}">
<a [routerLink]="['SomeRoute']" class="menu-item" ><span>Link1</span></a>
</li>
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但我注意到RouterLink 指令具有方法:isRouteActive。
这似乎是使用 this 操作链接类的明智方法。
但是我该如何使用它呢?
更新 RC.3:
在 RC.3 中routerLinkActive
添加了一个新指令。
routerLinkActive="classA classB class" [routerLinkActiveOptions]="{exact: true}"
Run Code Online (Sandbox Code Playgroud)
原来的:
Angular 路由器router-link-active
在活动路由器链接上自动设置类。
如果你想要自定义类,你可以使用类似的东西
也可以看看
归档时间: |
|
查看次数: |
1476 次 |
最近记录: |