Fernando Costa
Fernando Costa

Reputation: 699

How RouterLink and RouterLinkActive work?

Consider the following example:

<a routerLink="/user/bob" routerLinkActive="active-link">Bob</a>

The active-link CSS class will be added to the a tag when the current URL is either "/user" or "/user/bob",

How could I enforce an exact match?

Upvotes: 5

Views: 14506

Answers (1)

Lon Yang
Lon Yang

Reputation: 696

If you want to full match the url. You can add the routerLinkActiveOptions.

e.g.

<a [routerLink]="/user" routerLinkActive="active-link" [routerLinkActiveOptions]="{exact: true}">Bob</a>

Then only when the url is '/user' will add class. The '/user/bob' will not.

Upvotes: 9

Related Questions