Angular – How to fix: Property has no initializer and is not definitely assigned in the constructor

Angular

Angular is a platform and framework for building single-page client applications using HTML and TypeScript.

How to reproduce it

@Input() parentData: string;

Error

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

Solution #1

Add the ! because the input is expecting the value to be passed. In this case, there is no default value. The exclamation point is called the non-null assertion operator and it tells the TypeScript compiler that the value of this property won’t be null or undefined.

@Input() parentData!: string;

Solution #2

Use the ?.

@Input() parentData?: string;
https://www.mldgroup.com

Vyštudovaný top manažér pôsobiaci najmä ako manažér, marketér, softvérový inžinier, konzultant, bloger, YouTuber a zatiaľ neúspešný hudobník a producent. V rámci praxe pôsobil v rôznych odvetviach na rôznych pozíciách v malých aj veľkých firmách, vrátane spoluprác a partnerstiev s významnými firmami či poradenskými spoločnosťami.

Pridaj komentár

Vaša e-mailová adresa nebude zverejnená. Vyžadované polia sú označené *