2017-06-12 33 views
0

我想將活動類分配給angular2中的li標籤,但卡住了。Angular2將活動類分配給li標籤

這是我曾嘗試

<li [class.active]=" _router.isRouteActive(_router.generate(['/dashboard']))" > 

    //the above _router.isRouteActive throws an error 

    <a routerLink="/dashboard"> 
    <i class="icon-display4 position-left"></i> Dashboard</a> 
</li> 

在我的TS文件,我有

export class SecondaryNavComponent implements OnInit { 

    //thinking of using to determine active link 

constructor(public _router:Router) { } //tried also with private 

ngOnInit() { 
} 

}

錯誤讀取

ERROR TypeError: co._router.generate is not a function 

ALse webstorm generates an error that 
unkwon method router is active 

出了什麼問題?

回答

1

只需使用RouterLinkActive指令此:

https://angular.io/docs/ts/latest/api/router/index/RouterLinkActive-directive.html

這已經是@angular/router包內。

+0

routerLink活動指令在a標籤上設置活動類但不在外部li –

+0

它還可以在routerLink的祖先上設置活動類,閱讀類描述部分,示例 – cyrix

+0

謝謝我在零件上看到帶..............最後,您可以將RouterLinkActive指令應用於RouterLink的祖先。 –