Hi I am new to angular and i am getting this this error message " Property 'formGroup' has no initializer and is not definitely assigned in the constructor " but i have declared formGroup and i don't why i get this error
import { FormControl, Validators, FormGroup } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { AuthServiceService } from '../auth-service.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
formGroup:FormGroup;
constructor(private authService:AuthServiceService) { }
ngOnInit(): void {
this.initForm();
}
initForm(){
this.formGroup = new FormGroup({
email: new FormControl('',[Validators.required]),
password: new FormControl('',[Validators.required])
})
}
loginProces(){
if(this.formGroup.valid){
this.authService.login(this.formGroup.value).subscribe(result=>{
if(result.success){
console.log(result);
alert(result.message);
} else {
alert(result.message);
}
})
}
alert("hi");
}
}
CodePudding user response:
no, you have to initialize every variable or assign it in the constructor since you have strict mode activated. You can fix your error like this:
constructor(private authService:AuthServiceService) {
this.initForm();
}
ngOnInit(): void {}
CodePudding user response:
Just mark it as existing value:
export class LoginComponent implements OnInit {
formGroup!: FormGroup;
constructor...
CodePudding user response:
Just assign the value at the variable definition.
export class LoginComponent implements OnInit {
formGroup:FormGroup = new FormGroup({
email: new FormControl('',[Validators.required]),
password: new FormControl('',[Validators.required])
});