Javascript 角度材料:MatDatepicker:未找到 DateAdapter 的提供程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49720908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:27:56  来源:igfitidea点击:

Angular material: MatDatepicker: No provider found for DateAdapter

javascriptangularangular-material

提问by ajgisme

I'm trying to use the Datepicker component in Angular Material. Here is my HTML code:

我正在尝试在 Angular Material 中使用 Datepicker 组件。这是我的 HTML 代码:

<input matInput [matDatepicker]="picker" placeholder="Choose a date" disabled>
<mat-datepicker #picker disabled="false"></mat-datepicker>

However, it's not working for me and I'm getting the following error:

但是,它对我不起作用,并且出现以下错误:

Error: MatDatepicker: No provider found for DateAdapter.

错误:MatDatepicker:未找到 DateAdapter 的提供程序。

The error message tells me that I need to import MatNativeDateModule as well as MatDatepickerModule but I have done that. Here is my import code below from app.module.ts:

错误消息告诉我需要导入 MatNativeDateModule 和 MatDatepickerModule 但我已经这样做了。下面是我从 app.module.ts 导入的代码:

import {
    MatFormFieldModule,
    MatMenuModule,
    MatCheckboxModule,
    MatIconModule,
    MatDatepickerModule,
    MatNativeDateModule
} from '@angular/material';

Is there anything else I'm missing?

还有什么我想念的吗?

回答by Sajeetharan

You need to import both MatDatepickerModuleand MatNativeDateModuleunder imports and add MatDatepickerModuleunder providers

您需要同时导入MatDatepickerModuleMatNativeDateModule下导入并添加 MatDatepickerModule下提供者

 imports: [
    MatDatepickerModule,
    MatNativeDateModule 
  ],
  providers: [  
    MatDatepickerModule,  
  ],

回答by Sushil

Just import the

只需导入

MatNativeDateModule

MatNativeDateModule

along with the

随着

MatDatepickerModule

MatDatepicker 模块

at the app.module.ts or app-routing.module.ts.

在 app.module.ts 或 app-routing.module.ts。

@NgModule({
imports: [
    MatDatepickerModule,
    MatNativeDateModule 
]
})

回答by lilan silva

Angullar 8,9

角 8,9

import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material/core';

Angular 7 and below

Angular 7 及以下

import { MatDatepickerModule, MatNativeDateModule } from '@angular/material';

You need to import both MatDatepickerModule and MatNativeDateModule under imports and add MatDatepickerModule under providers

您需要在导入下导入 MatDatepickerModule 和 MatNativeDateModule 并在提供者下添加 MatDatepickerModule

imports: [
    MatDatepickerModule,
    MatNativeDateModule 
  ],
  providers: [  
    MatDatepickerModule,
    MatNativeDateModule  
  ],

回答by Tomasz Kula

Official docs: Error: MatDatepicker: No provider found for DateAdapter/MAT_DATE_FORMATS

官方文档:错误:MatDatepicker:未找到 DateAdapter/MAT_DATE_FORMATS 的提供程序

The datepicker was built to be date implementation agnostic. This means that it can be made to work with a variety of different date implementations. However it also means that developers need to make sure to provide the appropriate pieces for the datepicker to work with their chosen implementation. The easiest way to ensure this is just to import one of the pre-made modules: MatNativeDateModule, MatMomentDateModule.

日期选择器被构建为与日期实现无关。这意味着它可以用于各种不同的日期实现。然而,这也意味着开发人员需要确保为日期选择器提供合适的部分来处理他们选择的实现。确保这一点的最简单方法是导入预制模块之一:MatNativeDateModule、MatMomentDateModule。

Fix:

使固定:

@NgModule({
  imports: [MatDatepickerModule, MatNativeDateModule],
})
export class MyApp {}