-1

我导入了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.
4

1 回答 1

1

您还应该导入MatInputModule

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

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

否则 Angular 不知道如何初始化<input matInput提供的指令MatFormFieldControl

@Directive({
  selector: `input[matInput], textarea[matInput], select[matNativeControl],
      input[matNativeControl], textarea[matNativeControl]`,
  ...
  providers: [{provide: MatFormFieldControl, useExisting: MatInput}],
})
于 2019-08-16T10:07:05.730 回答