Search code examples
angularroutesng-class

Add a Class when In a certain Route - Angular 2


I'm trying to add a class when Im on a certain route. The code is in my AppComponent, Im using ngClass.

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

and then I have the function on the same app.component.ts

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

The error I'm getting is the following one:

Property 'router' does not exist on type 'AppComponent'

And yes, I am importing Routes, RouterModule and Router on the header. Can someone help me?

Thanks in advance


Solution

  • You need to inject the router

      export class AppComponent  { 
    
        constructor(private router:Router) {}
    
        getRoute(){
         if (this.router.url === '/atendimento'){