我正在尝试使用isRouteActive
函数在点击其中包含的链接时切换active
元素的li
类,但它会一直给我{{1}这样的错误}}。该文件如下所示:
TypeError: Cannot read property 'isRouteActive' of undefined in [isRouteActive(['./Home']) in AppComponent@6:16]
我试过各种各样的路径,比如'/ home','#/ home','/ Home','。/ Home','Home',但它们都不起作用。这个错误可能与什么有关?以及如何实际调试这种情况?
答案 0 :(得分:4)
刚刚发布了问题,最后找到了答案:忘了放constructor
并在其中提供Router
。因此,工作版本更改export class Appcomponent
部分并按以下方式查看:
export class AppComponent {
constructor(private router: Router) {
}
public isRouteActive(route) {
return this.router.isRouteActive(this.router.generate(route))
}
}
另见the link。
答案 1 :(得分:2)