错误错误:使用API​​文档时,mat-form-field必须包含MatFormFieldControl吗?

时间:2019-08-16 10:03:27

标签: javascript angular typescript angular-material angular-material-8

我这样导入MatFormFieldModule

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';

    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    //import { MaterialModule } from './material/material.module';
    import {MatFormFieldModule} from '@angular/material/form-field';

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        BrowserAnimationsModule,
    //    MaterialModule,
        MatFormFieldModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

我在app.component.html内(直接取自API文档):

    <mat-form-field>
      <input matInput placeholder="Input">
    </mat-form-field>

ERROR Error: mat-form-field must contain a MatFormFieldControl.

有什么想法吗?

我得到了错误:

ERROR Error: mat-form-field must contain a MatFormFieldControl.

1 个答案:

答案 0 :(得分:1)

您还应该导入MatInputModule

import {MatInputModule} from '@angular/material/input';

@NgModule({
  imports: [
    MatFormFieldModule,
    MatInputModule,
    ...
  ]
})

否则Angular不知道如何为提供<input matInput的{​​{1}}初始化指令:

MatFormFieldControl