简体   繁体   English

Angular7:NullInjectorError:没有 FormGroup 的提供者

[英]Angular7: NullInjectorError: No provider for FormGroup

I'm getting really fustrated because I have no idea what's happening.我真的很沮丧,因为我不知道发生了什么。 Everything worked correctly this morning right before some changes I made to merge together 2 forms in a ReactiveForm and now I get the following error in the browser:今天早上一切正常,就在我进行了一些更改以将 ReactiveForm 中的 2 个表单合并在一起之前,现在我在浏览器中收到以下错误:

Error: StaticInjectorError(AppModule)[FormGroup]:错误:StaticInjectorError(AppModule)[FormGroup]:
StaticInjectorError(Platform: core)[FormGroup]: NullInjectorError: No provider for FormGroup! StaticInjectorError(Platform: core)[FormGroup]: NullInjectorError: No provider for FormGroup! Error: StaticInjectorError(AppModule)[FormGroup]:错误:StaticInjectorError(AppModule)[FormGroup]:
StaticInjectorError(Platform: core)[FormGroup]: NullInjectorError: No provider for FormGroup! StaticInjectorError(Platform: core)[FormGroup]: NullInjectorError: No provider for FormGroup!

I'm importing FormsModule and ReactiveFormsModule in my app.module.ts file:我在app.module.ts文件中导入FormsModuleReactiveFormsModule

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
[...]
@NgModule({
  declarations: [
    AppComponent,
    CustomersComponent,
    HeaderComponent,
    CustomersListComponent,
    CustomerEditComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    ReactiveFormsModule,
    ...
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

And then the FormGroup in my component:然后是我组件中的 FormGroup:

import { FormGroup, FormControl, Validators } from '@angular/forms';
[...]

And declaring a new FormGroup afterwards in the component.然后在组件中声明一个新的 FormGroup 。 I tried to reinstall the @angular/forms package with npm but I'm still getting the error... I've seen some similar questions asked but from what I could tell, it was related with the testing environment.我试图用 npm 重新安装@angular/forms包,但我仍然收到错误......我看到了一些类似的问题,但据我所知,它与测试环境有关。 If you have any idea, thanks in advance.如果您有任何想法,请提前致谢。

Don't inject FormGroup into the constructor of the component.不要将 FormGroup 注入到组件的构造函数中。

This it's NOT ok:这不行:

constructor(private fb:FormBuilder,  private ts:FormGroup)

This its ok:这没关系:

constructor(private fb:FormBuilder)

This problem happens when the input is not a form control, which is not related to any Angular version or Form Builder.当输入不是表单控件时会发生此问题,该控件与任何 Angular 版本或 Form Builder 无关。

To solve this apply either of the below solutions,要解决此问题,请应用以下任一解决方案,

  1. form: FormGroup |表单:FormGroup | any任何
  2. add "strictPropertyInitialization": false, in tsconfig.json file .在 tsconfig.json 文件中添加“strictPropertyInitialization”:false。

It got resolved for me like this :)它像这样为我解决了:)

I would have to see your ts file where the form is being built.我必须查看正在构建表单的 ts 文件。 But it sounds like you are using Reactive Forms and have not imported everything needed.但听起来您正在使用 Reactive Forms 并且尚未导入所需的所有内容。 ReactiveForms requires not only: ReactiveForms 不仅需要:

 import { FormGroup, FormControl, Validators } from '@angular/forms';

but also be sure to import "FormBuilder" in that same import statement:但也一定要在同一个导入语句中导入“FormBuilder”:

import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';

also, in your constructor you should have similar to the following :此外,在您的构造函数中,您应该具有类似于以下内容:

constructor(private fb: FormBuilder) {} 

and the form should have the form builder instantiating the form:并且表单应该具有实例化表单的表单构建器:

productForm = this.fb.group({})

If that doesn't fix the issue, I would need to see more to help.如果这不能解决问题,我将需要看到更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM