0

I have the following link:

<a [routerLink]="['/posts', id, 'about']" routerLinkActive="active">Post</a>

And the following CSS:

a.active {
  color: orange;
}

The component that contains this HTML and CSS is:

@Component({
  selector: 'menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.less']
})

export class MenuComponent {

  @Input() menuType: string;

  constructor(private route: ActivatedRoute) { }

}

If I click the link the anchor has the class active and is orange.

But if I ran the the following command from another component:

  this.tagService.create(tagName).subscribe(

    (next) => { 
      this.router.navigate(['/posts', next.postId, 'about' ]);
    }

  );

In this case I am redirect to the page, the link has the active class but it is not orange.

Any idea what am I missing?

Update

After some tests I found out that the link becoming orange does not depend on using:

.active {color: orange;}
a.active {color: orange;}
a[routerLinkActive="active"] {color:orange;}

Consider the steps:

  1. Click on link /posts/1/about that goes to page /posts/1/about.
  2. Click on a link that opens a Modal with a form and click save that runs:

    this.tagService.create(tagName).subscribe(

    (next) => { 
      this.router.navigate(['/posts', next.postId, 'about' ]);
    }
    

    );

    I am redirected to /posts/1/about and the link is orange as expected.

  3. Click on a link that opens the Modal again and do the same as in (2).

    I am redirected as expected but now the menu is not orange ...

It seems that the way I got to the page before opening the modal, clicking a link or using this.router.navigate, makes the link to become orange or not after I use this.route.navigate a second time.

Isn't this strange?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • Where is your CSS defined? And what ViewEncapsulation are you using on your components? – Alex K Jan 09 '20 at 15:01
  • I am using the default option as I am not changing it. I suppose Emulated? The link I posted is in a component's menu html and the css is in the same component's menu less code. I updated my question with the component code. – Miguel Moura Jan 09 '20 at 15:08
  • try defining the css as just `.active{color:orange}`, see here for more https://angular.io/api/router/RouterLinkActive – Farasi78 Jan 09 '20 at 15:13
  • 2
    Can you create a StackBlitz to recreate the issue? – Alex K Jan 09 '20 at 15:14
  • When you inspect it and look at the style inheritances, is your css selector getting used but possibly being overridden? Is the CSS defined in the component or elsewhere? If it's in the component is the css wrapped in `:host { }`? – Chris W. Jan 09 '20 at 15:22
  • Can you try this? `a[routerLinkActive="active"]{color:orange;}` – Schwarz54 Jan 09 '20 at 15:42
  • @Farasi78 I tried many options such as `a.active { color: orange }`, `.active { color: orange }`, `a.active:link { color: orange }`and I noticed that a few times it appears orange but most of the time no ... It is not even consistent. Have no idea why. – Miguel Moura Jan 09 '20 at 15:43
  • @MiguelMoura stackbliz would be helpful. You could also try this way `Post` and in css `.active{color:orange}` – Farasi78 Jan 09 '20 at 15:46
  • 1
    https://stackoverflow.com/questions/39271654/routerlinkactive-for-routerlink-with-parameters-dynamic – Eliseo Jan 09 '20 at 15:47
  • @Eliseo, is something on the link you posted different from what I am doing? I cannot find the difference. – Miguel Moura Jan 09 '20 at 16:01
  • @Farasi78 I am just creating a Stackblitz example to try to replicate – Miguel Moura Jan 09 '20 at 16:09
  • @Farasi78 What I have is the following: https://stackblitz.com/edit/angular-popup-active but in this example it works fine ... so I am trying to figure what is different in my code to try to replicate it. – Miguel Moura Jan 09 '20 at 16:58
  • @MiguelMoura good deal... make sure you haven't got another css class '.active' that may be overriding your setting. Good luck! – Farasi78 Jan 09 '20 at 17:12

0 Answers0