Using Angular 11, I am facing an issue while creating a login form with ReactiveFormsModule. Upon loading the login page, a TypeError is displayed in the browser console. Additionally, after typing something in the input field, another TypeError appears. https://i.sstatic.net/XxMkB.png
Despite searching extensively for a solution and attempting all suggestions from others who have faced the same problem, I have not been able to resolve it. Having only started learning Angular a month ago, I suspect that I might be making a mistake somewhere but I am unable to pinpoint it.
login.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup} from '@angular/forms';
import { Router } from '@angular/router';
import { AuthService } from 'src/app/core/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
form: FormGroup;
constructor(
private fb: FormBuilder,
private authService: AuthService,
private router: Router
) {
this.form = this.fb.group({
email: [''],
password: ['']
})
}
ngOnInit(): void {
}
submitHandler(): void {
const data = this.form.value;
this.authService
.login(data)
.subscribe({
next: () => {
this.router.navigate(['/']);
},
error: (err) => {
console.error(err);
}
})
}
}
login.component.html
<section class="login-wrapper">
<form formGroup="form" class="login-form" (ngSubmit)="submitHandler()">
<article class="login-body">
<h1>Login</h1>
<input name="email" type="text" placeholder="E-mail" formControlName="email">
<input name="password" type="password" placeholder="Password" formControlName="password">
<button type="submit">Login</button>
<p>Don't have an account? <a href="/signup">Sign up</a></p>
</article>
</form>
</section>
user.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SignUpComponent } from './sign-up/sign-up.component';
import { LoginComponent } from './login/login.component';
import { UserRoutingModule } from './user-routing.module';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
SignUpComponent,
LoginComponent
],
imports: [
CommonModule,
UserRoutingModule,
FormsModule,
ReactiveFormsModule
]
})
export class UserModule { }
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CoreModule } from '../app/core/core.module';
import { HeaderComponent } from '../app/core/header/header.component';
import { FooterComponent } from '../app/core/footer/footer.component';
import { HomeComponent } from './home/home.component';
import { UserRoutingModule } from '../app/user/user-routing.module';
import { UserModule } from './user/user.module';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent,
HomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
CoreModule,
UserRoutingModule,
UserModule,
HttpClientModule
],
providers: [],
bootstrap: [
AppComponent,
HeaderComponent,
FooterComponent
]
})
export class AppModule { }