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

可能出了什么问题?

只需使用 RouterLinkActive 指令即可:

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

它已经在@angular/router包内。

最新更新