在特定路线中添加类别-Angular 2 [英] Add a Class when In a certain Route - Angular 2

查看:58
本文介绍了在特定路线中添加类别-Angular 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在特定路线上添加Im的课程.代码在我的AppComponent中,我使用ngClass.

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....
    })

然后我在同一app.component.ts上具有该功能

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:

类型"AppComponent"上不存在属性路由器"

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

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

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

预先感谢

推荐答案

您需要注入路由器

  export class AppComponent  { 

    constructor(private router:Router) {}

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

这篇关于在特定路线中添加类别-Angular 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆