0

I am using Angular 7. I have problem with routerLinkActive. When i chose link in navbar, it gives .active class to link, but when i click somewhere else on page link loses his style, but in console active class is still active. So how to keep active class style on page link where i am located. I done this so many times and never had problem, tryed to copy working code but still no progress.

console.log app.module.ts

import { RouterModule } from "@angular/router";
RouterModule.forRoot([
      {
        path: "",
        component: DashboardComponent,
        canActivate: [AuthGuardService]
      },
      { path: "login", component: LoginComponent },
      { path: "register", component: RegisterComponent },
      {
        path: "pacijent/:id",
        component: PacijentComponent,
        canActivate: [AuthGuardService]
      },

      {
        path: "pacijent/:id/edit",
        component: PacijentEditComponent,
        canActivate: [AuthGuardService]
      },
      {
        path: "istorija/:id/:id",
        component: NalazComponent,
        canActivate: [AuthGuardService]
      },
      {
        path: "obavestenja",
        component: ObavestenjaComponent,
        canActivate: [AuthGuardService]
      },
      {
        path: "users/:id",
        component: ProfilComponent,
        canActivate: [AuthGuardService]
      },

      { path: "**", component: NotfoundComponent }
    ])

html

  <li *ngIf="isLoggedIn">
        <a
          routerLinkActive="active"
          [routerLinkActiveOptions]="{ exact: true }"
          routerLink=""
          ><i class="fas fa-home"></i> Glavna Strana</a
        >
      </li>
  <li *ngIf="isAdmin">
        <a
          routerLinkActive="active"
          [routerLinkActiveOptions]="{ exact: true }"
          routerLink="obavestenja"
          ><i class="fas fa-signal"></i> Statistika</a
        >
      </li>

css

.nav-right .nav li a:hover,
.nav-right .nav li a:focus {
  font-size: 24px;
  text-decoration: none;
  color: #0078ad;
  background: white;
}
.nav-right .nav li a .active {
  font-size: 24px;
  text-decoration: none;
  color: #0078ad;
  background: white;
}
Viktor Molnar
  • 99
  • 3
  • 13

2 Answers2

0

Try using absolute paths

routerLink="/"
routerLink="/obavestenja"
Harout
  • 66
  • 4
0
.nav-right .nav li a.active {
  font-size: 24px;
  text-decoration: none;
  color: #0078ad;
  background: white;
}

I had space between a and .active Sorry for wasting time...

Viktor Molnar
  • 99
  • 3
  • 13