Home > Net >  How to solve a typescript initialization issue in my Angular project?
How to solve a typescript initialization issue in my Angular project?

Time:12-19

I have an Angular project and I'm trying to define a variable with the following type:

navbarLinks: QueryList<ElementRef>;

but I get the following error:

Property 'navbarLinks' has no initializer and is not definitely assigned in the constructor.

30   navbarLinks: QueryList<ElementRef>;

Now, I don't want to use this as a solution:

navbarLinks!: QueryList<ElementRef>;

Is there any other way I can fix this?

CodePudding user response:

If you really do not want to set a default value to this property you need to adjust your type to

navbarLinks?: QueryList<ElementRef>;

which is the same as

navbarLinks: QueryList<ElementRef> | undefined;

Another solution would be to give the property a default value. As I don't know what the QueryList<T> interface looks like, I can't really give you an example.

  • Related