Angular2将活动类分配给li标记

时间:2017-06-12 19:37:23

标签: angular typescript angular2-routing

我想将活动类分配给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 个答案:

答案 0 :(得分:1)

只需使用RouterLinkActive指令:

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

它已经在@angular/router包中。