I want to select <a>
element by className when its router link is active, but it returns null
navbar.component.html code:
<nav class="profile-navbar">
<ul>
<li class="posts-item">
<a
[routerLink]="['/app/profile', username, 'posts']"
routerLinkActive="active-link"
>Posts</a
>
</li>
<li class="images-item">
<a
[routerLink]="['/app/profile', username, 'images']"
routerLinkActive="active-link"
>Images</a
>
</li>
</ul>
</nav>
navbar.component.ts code:
import { AfterViewInit, Component, OnInit } from '@angular/core';
@Component({
selector: 'profile-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnInit, AfterViewInit {
constructor() {
}
ngOnInit(): void {
}
ngAfterViewInit(): void {
const activeLink = document.querySelector('.active-link');
console.log(activeLink)
}
}
How can I select the element the element based on routerLinkActive class??