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

时间:2023-03-09 19:34:50
Can't bind to 'formGroup' since it isn't a known property of 'form'

在APP.module.ts中引入FormsModule, ReactiveFormsModule。

 import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { LoginModule } from './login/login.module';
import { AppRoutingModule } from './app.route.module'; @NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
CoreModule,
FormsModule,
ReactiveFormsModule,
LoginModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

特别注意:在HTML中一定不能用form,随便换一个单词myGroup就行。

[formGroup]="myGroup"
 <div class="login-wrap">
<form [formGroup]="myGroup" (ngSubmit)="onSubmit(myGroup,$event)">
<mat-card class="example-card">
<mat-card-header><mat-card-title>登录</mat-card-title></mat-card-header>
<mat-card-content>
<mat-form-field>
<input matInput placeholder="您的email" formControlName="email">
</mat-form-field>
<mat-form-field>
<input matInput placeholder="您的密码" formControlName="password">
</mat-form-field>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button type="submit">登录</button>
</mat-card-actions>
<mat-card-footer>
<p>还没有账户?<a href="#">注册</a></p>
<p>忘记密码?<a href="#">找回</a></p>
</mat-card-footer>
</mat-card>
</form>
<mat-card class="example-card">
<mat-card-header>
<mat-card-title>每日佳句</mat-card-title>
<mat-card-subtitle>满足感是在于不断努力,而不是现有成就。全新努力就会胜利满满。</mat-card-subtitle>
</mat-card-header>
<img mat-card-image src="https://material.angular.io/assets/img/examples/shiba2.jpg" alt="Photo of a Shiba Inu">
<mat-card-content>
<p>
The Shiba Inu is the smallest of the six original and distinct spitz breeds of dog from Japan.
A small, agile dog that copes very well with mountainous terrain, the Shiba Inu was originally
bred for hunting.
</p>
</mat-card-content>
</mat-card>
</div>