Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't construct a query for the property, since the query selector wasn't defined

Tags:

angular

Currently have this problem with a component in Angular 2 that exists of other components. The components of the 'main' component can exist multiple times in the hierarchy.

But i am getting this error: "Can't construct a query for the property "navComponent" of "SidenavLinkComponent" since the query selector wasn't defined"

SidenavLinkComponent: @ContentChild(SidenavNavComponent) navComponent: SidenavNavComponent;  SidenavNavComponent: @ContentChildren(SidenavLinkComponent) linkComponents: QueryList<SidenavLinkComponent>; 

I have made this slim plunker, where the problem is shown: Plunker

I have no idea why it happens.

like image 548
Blom Avatar asked Mar 20 '17 13:03

Blom


1 Answers

It's because of a circular dependency between SidenavComponent and SidenavNavComponent. It can be resolved using forwardRef. Don't forget to import it as well:

import { forwardRef } from '@angular/core'; 

@ViewChild(forwardRef(() => SidenavNavComponent)) private navComponent: SidenavNavComponent; 

Plunker example

like image 80
Günter Zöchbauer Avatar answered Sep 22 '22 21:09

Günter Zöchbauer