While trying to run the angular app with login form, defined as shown below, it throws an Error:
src/app/pages/auth/login/login.component.ts:12:3 - error TS2564: Property 'loginForm' has no initializer and is not definitely assigned in the constructor.
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { AuthService } from '../../../services/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;
constructor(private authService: AuthService) {}
ngOnInit() {
this.loginForm = new FormGroup({
email: new FormControl('', {
validators: [Validators.required, Validators.email]
}),
password: new FormControl('', { validators: [Validators.required] })
});
}
onSubmit() {
this.authService.login({
email: this.loginForm.value.email,
password: this.loginForm.value.password
});
console.log("logging in...");
}
}
Does anyone know what is the correct way to initialize FormGroup?