If I add a content children to my component
@ContentChildren(RouterLink, { descendants: true })
links: QueryList<RouterLink>;
and have a template
<a [routerLink]="''">Link 1</a><br>
<a [routerLink]="''">Link 2</a><br>
<a [routerLink]="''">Link 3</a><br>
Number of links: {{links.length}}
Links length is always 0.
I have tried with a directive as well
@Directive({
selector: '[appRouterLinkWatch]'
})
export class RouterLinkWatchDirective implements AfterContentInit {
@ContentChildren(RouterLink, { descendants: true })
links: QueryList<RouterLink>;
constructor(@Optional() private link: RouterLink) {}
ngAfterContentInit() {
console.log(this.links.length, this.link);
}
}
Both the links content children and the link dependency are not populated. What am I doing wrong to get a list of router links in my component?
I have been looking at the sorce code for the routerLinkActive directive https://github.com/angular/angular/blob/master/packages/router/src/directives/router_link_active.ts I cannot see what I am doing differently.
I have a stack blitz here https://stackblitz.com/edit/angular-ivy-bvpyhd?file=src%2Fapp%2Fapp.component.html