typescript MatDatepicker:未找到 DateAdapter 的提供程序。您必须在应用程序根目录中导入以下模块之一:MatNativeDateModule
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56503946/
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
MatDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MatNativeDateModule
提问by Ashutosh Soni
I have imported the matdatepicker in my app.module.ts file but still it shows some error. I can't get the material component working. the button works fine and uses angular material component. But when I use datepicker it doesn't work.
我已经在我的 app.module.ts 文件中导入了 matdatepicker,但它仍然显示一些错误。我无法让材料组件工作。按钮工作正常并使用角材料组件。但是当我使用 datepicker 时它不起作用。
app.module.ts file
app.module.ts 文件
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import{ HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatTableModule} from '@angular/material/table';
import {MatDatepickerModule} from '@angular/material/datepicker';
import {MatButtonModule} from '@angular/material/button';
import {MatFormFieldModule} from '@angular/material/form-field';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { InactiveusersComponent } from './component/inactiveusers/inactiveusers.component';
import { CalendarComponent } from './component/calendar/calendar.component';
import { InactiveItemComponent } from './component/inactive-item/inactive-item.component';
import { NewtableComponent } from './component/newtable/newtable.component';
@NgModule({
declarations: [
AppComponent,
InactiveusersComponent,
CalendarComponent,
InactiveItemComponent,
NewtableComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
BrowserAnimationsModule,
MatTableModule,
MatDatepickerModule,
MatButtonModule,
MatFormFieldModule
],
exports:[
MatDatepickerModule,
MatButtonModule
],
providers: [MatFormFieldModule,MatDatepickerModule],
bootstrap: [AppComponent]
})
export class AppModule { }
calendar.component.html
日历.component.html
<input #startDate type="date" name="startdate">
<input #endDate type="date"name="enddate">
<button (click)="sendRange(startDate.value,endDate.value)" mat-button color="primary">Submit</button>
<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>
ERROR Error: MatDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MatNativeDateModule, MatMomentDateModule, or provide a custom implementation.
at createMissingDateImplError (datepicker.js:37)
at new MatDatepickerInput (datepicker.js:2344)
at createClass (core.js:24577)
at createDirectiveInstance (core.js:24386)
at createViewNodes (core.js:34994)
at callViewAction (core.js:35444)
at execComponentViewsAction (core.js:35349)
at createViewNodes (core.js:35023)
at callViewAction (core.js:35444)
at execComponentViewsAction (core.js:35349)
回答by DEVolkan
I've solved it by adding follow modules:
我通过添加以下模块解决了它:
import {MatNativeDateModule} from '@angular/material';
import { MatMomentDateModule } from "@angular/material-moment-adapter";
and in your imports
并在您的进口
imports: [
...
MatDatepickerModule,
MatButtonModule,
MatFormFieldModule,
MatNativeDateModule, MatMomentDateModule,
],
I'm not that deep in material-angular I've only follow the error message advice
我在材料角度方面没有那么深,我只遵循错误消息建议
ERROR Error: MatDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MatNativeDateModule, MatMomentDateModule, or provide a custom implementation.
错误 错误:MatDatepicker:未找到 DateAdapter 的提供程序。您必须在应用程序根目录中导入以下模块之一:MatNativeDateModule、MatMomentDateModule,或提供自定义实现。
回答by Madhusanka Edirimanna
I got, Here working fine Just import the modules in APP.MODULE.TS file
我明白了,这里工作正常只需导入 APP.MODULE.TS 文件中的模块
import {MatNativeDateModule} from '@angular/material/core';
imports: [
...
MatNativeDateModule
],
回答by Roberto C. Rodriguez-Hidalgo
I just used the first import referred:
我只是使用了第一个导入引用:
import {MatNativeDateModule} from '@angular/material';
the other import got me to another error I didn't solved, but it wasn't necessary.
另一个导入让我遇到了另一个我没有解决的错误,但这不是必需的。
Don't forget to add MatNativeDateModule to the imports array in app.module.ts.
不要忘记将 MatNativeDateModule 添加到 app.module.ts 中的导入数组。
回答by mnojind
I got, Here working fine Just import the modules in APP.MODULE.TS file
我明白了,这里工作正常只需导入 APP.MODULE.TS 文件中的模块
import {MatDatepickerModule} from '@angular/material/datepicker';
import {MatNativeDateModule} from '@angular/material/core';
import {MatInputModule} from '@angular/material/input';
imports: [
MatDatepickerModule
,MatNativeDateModule,MatInputModule
]
HTML :
You need to wrap element with
Tag
<form>
<mat-form-field>
<mat-label>Choose a date</mat-label>
<input matInput [matDatepicker]="picker">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>