Can't bind to 'formGroup' since it isn't a known property of 'form'错误解

2021-07-08 10:13:36 浏览数 (1)

最近学习了下angular,利用Angular CLI 在本地搭建了个环境,然后装了个ant.design的NG-ZORRO库,试了下用ui的脚手架来创建组件

代码语言:javascript复制
ng g ng-zorro-antd:form-normal-login login

创建完成发现访问项目的时候,在使用nz-form时报错Can't bind to 'formGroup' since it isn't a known property of 'form',百度了下解决方案:

需要从@angular/forms导入ReactiveFormsModule。因为FormGroupDirective 指令属于ReactiveFormsModule一部分。

导入后的app.module.ts

代码语言:javascript复制
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NewsComponent } from './news/news.component';
import { NgZorroAntdModule, NZ_I18N, zh_CN } from 'ng-zorro-antd';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { registerLocaleData } from '@angular/common';
import zh from '@angular/common/locales/zh';
import { LoginComponent } from './login/login.component';

registerLocaleData(zh);

@NgModule({
  declarations: [
    AppComponent,
    NewsComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgZorroAntdModule,
    FormsModule,
    HttpClientModule,
    BrowserAnimationsModule,
    ReactiveFormsModule
  ],
  providers: [{ provide: NZ_I18N, useValue: zh_CN }],
  bootstrap: [AppComponent]
})
export class AppModule { }

0 人点赞