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

前端之家收集整理的这篇文章主要介绍了在特定路线中添加类 – 角度2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在某条路线上添加课程.代码在我的AppComponent中,我使用的是ngClass.

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

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

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

我得到的错误如下:

Property ‘router’ does not exist on type ‘AppComponent’

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

提前致谢

解决方法

你需要注入路由器

export class AppComponent  { 

    constructor(private router:Router) {}

    getRoute(){
     if (this.router.url === '/atendimento'){

猜你在找的Angularjs相关文章