在特定路线中添加类 - 角度2

Ren*_*ima 5 routing ng-class angular

我想在某条路线上添加课程.代码在我的AppComponent中,我使用的是ngClass.

    @Component({
     selector: 'my-app',
     template: `<a [ngClass]="getRoute(router)">
       // Some html code....
    })
Run Code Online (Sandbox Code Playgroud)

然后我在相同的app.component.ts上有这个功能

  export class AppComponent  { 
    getRoute(){
     if (this.router.url === '/atendimento'){
      return "hide-bar";
   }
  }
 }
Run Code Online (Sandbox Code Playgroud)

我得到的错误如下:

'AppComponent'类型中不存在属性'router'

是的,我在标题上导入Routes,RouterModule和Router.有人能帮我吗?

提前致谢

Gün*_*uer 5

需要注入路由器

  export class AppComponent  { 

    constructor(private router:Router) {}

    getRoute(){
     if (this.router.url === '/atendimento'){
Run Code Online (Sandbox Code Playgroud)