datepicker - Uncaught Error : Unexpected directive 'MatFormField' imported by the module 'AppModule' . 请添加 @NgModule 注释

标签 datepicker angular-material angular7

我想使用 Angular Material 日期选择器,但出现此错误。

Uncaught Error: Unexpected directive 'MatFormField' imported by the module 'AppModule'. Please add a @NgModule annotation.



app.component.html
<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import {MatFormField} from '@angular/material';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    MatFormField
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

最佳答案

要导入组件和指令等,您导入它们的模块,而不是实际的组件和指令。所以需要导入MatFormFieldModule而不是 MatFormField :

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

import { AppComponent } from './app.component';
import { MatFormFieldModule } from '@angular/material/form-field';

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

关于datepicker - Uncaught Error : Unexpected directive 'MatFormField' imported by the module 'AppModule' . 请添加 @NgModule 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53391255/

相关文章:

angular - 如何在 <mat-select> 组件中实现自动完成?

angular - 单个页面中的多个 Mat-Paginator(Angular 5 数据表)

javascript - Angular 7获取关键字列表的谷歌搜索结果计数

javascript - 限制日期时间选择器上的过去日期

html - 如何使用 `dir="auto "` with Angular Material' s textarea?

datepicker - 获取错误无法测试类型为 'void' 的表达式的真实性角度 7

Angular 7+,从父组件调用子组件的方法好吗?

javascript - 在 React 中禁用 Material ui 日历中的特定日期

php - 单击按钮打开多日期选择器

JavaScript |使用变量添加月份至今时出现错误结果