我正在使用Angular 9。
我收到以下错误:
没有FormBuilder的提供者
在许多情况下都已报告它,并且一般的解决方案似乎是将添加FormsModule
到app.module.ts
文件中。
例如
import { FormsModule } from '@angular/forms';
imports: [
FormsModule
我已经尝试过了,但是仍然出现错误。
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[FormBuilder -> FormBuilder -> FormBuilder]: NullInjectorError: No provider for FormBuilder! NullInjectorError: R3InjectorError(AppModule)[FormBuilder -> FormBuilder -> FormBuilder]: NullInjectorError: No provider for FormBuilder!
题
我可能正在做一些在Angular 9中更改过的事情吗?我需要怎么做才能解决此问题?
我的代码:
login.component.ts
import { Component, OnInit } from '@angular/core';
import {Router} from '@angular/router';
import { FormGroup, FormBuilder } 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, private formBuilder: FormBuilder, private router: Router) {
}
ngOnInit(): void {
this.loginForm = this.formBuilder.group({
username: [''],
password: ['']
});
}
get f() { return this.loginForm.controls; }
login() {
this.authService.login(
{
username: this.f.username.value,
password: this.f.password.value
}
)
.subscribe(success => {
if (success) {
this.router.navigate(['/secret-random-number']);
}
});
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LoginComponent } from './auth/containers/login/login.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FlexLayoutModule } from '@angular/flex-layout';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatToolbarModule } from '@angular/material/toolbar';
import { FormsModule } from '@angular/forms';
import { ApprovalListComponent } from './approval-list/component/approval-list.component';
import { MatTableModule } from '@angular/material/table';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MatTableExporterModule } from 'mat-table-exporter';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
ApprovalListComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
BrowserAnimationsModule,
FlexLayoutModule,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
MatCardModule,
MatToolbarModule,
FormsModule,
MatTableModule,
MatPaginatorModule,
MatSortModule,
MatTableExporterModule
],
providers: [LoginComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
表单生成器为 ReactiveFormsModule
import { ReactiveFormsModule } from '@angular/forms';
imports: [
ReactiveFormsModule
您还需要此模块才能使用formGroup
和formControl
指令。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句