Angular 尝试使用ReactiveFormsModule创建登录表单时出现两个类型错误
我目前正在使用Angular 11并尝试使用ReactiveFormsModule创建一个登录表单。 登录页面加载后,浏览器控制台中立即抛出TypeError。 在输入字段中键入内容后,会出现另一个TypeError。 我在整个网络上搜索了一个解决方案,并尝试了其他人提出的关于同一问题的所有建议,但没有一个能解决我的问题。我一个月前才开始学习Angular,我想我做错了什么,但我真的找不到它是什么 login.component.tsAngular 尝试使用ReactiveFormsModule创建登录表单时出现两个类型错误,angular,typescript,Angular,Typescript,我目前正在使用Angular 11并尝试使用ReactiveFormsModule创建一个登录表单。 登录页面加载后,浏览器控制台中立即抛出TypeError。 在输入字段中键入内容后,会出现另一个TypeError。 我在整个网络上搜索了一个解决方案,并尝试了其他人提出的关于同一问题的所有建议,但没有一个能解决我的问题。我一个月前才开始学习Angular,我想我做错了什么,但我真的找不到它是什么 login.component.ts import { Component, OnInit }
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);
}
})
}
}
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 { }
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 { }
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>
应用程序模块.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);
}
})
}
}
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 { }
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 { }
您忘记了表单html中
formGroup
周围的括号:
<form [formGroup]="form" class="login-form" (ngSubmit)="submitHandler()">
这解决了我的问题!非常感谢!:)不客气:)