Angular - Property has no initializer and is not definitely assigned in the constructor.
Hi,
In this tute, we will discuss angular property has no initializer and is not definitely assigned in the constructor. This article will give you simple example of angular formgroup has no initializer and is not definitely assigned in the constructor. let’s discuss about viewchild property has no initializer and is not definitely assigned in the constructor solved.
Few days ago i was working on my angular 13 app and i was getting following error for define id:
"Property 'id' has no initializer and is not definitely assigned in the constructor."
because i was using my angular 12 code. I will show what was that code and what is solution. let's see both and also look solution:
Old Code:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.css']
})
export class ViewComponent implements OnInit {
id: number;
}
Solution: you need to add "!" after variable
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.css']
})
export class ViewComponent implements OnInit {
id!: number;
}
If you are creating interface then you can solve by following ways.
Solution 1:
export interface Post {
id!: number;
title!: string;
}
Solution 1:
export interface Post {
id: number = '';
title: string = '';
}
I hope it can help you....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular Material Multi Select Dropdown with Chips Example
- Angular Material Carousel Slider Example
- Angular Material Phone Number Input Mask Example
- Angular 11 Reactive Forms Validation Example
- How to use Bootstrap Datepicker in Angular?
- Angular FormGroup Example Tutorial
- How to Get Query String from URL in Angular?
- Angular 9/8 HttpClient for Sending Http Request Example
- Angular Get Current Route Name Example