javascript - MatDatepicker: ไม่พบผู้ให้บริการสำหรับ DateAdapter คุณต้องนำเข้าหนึ่งในโมดูลต่อไปนี้ที่รากของแอปพลิเคชันของคุณ: MatNativeDateModule
ฉันได้ทำการนำเข้า matdatepicker ในไฟล์ app.module.ts ของฉันแล้ว แต่ก็ยังแสดงข้อผิดพลาดอยู่ ฉันไม่สามารถทำให้คอมโพเนนต์ material ทำงานได้ ปุ่มทำงานได้ปกติและใช้คอมโพเนนต์ของ Angular Material แต่เมื่อฉันใช้ datepicker มันไม่ทำงาน
ไฟล์ 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
<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)
คำตอบ #1
ฉันได้แล้ว ที่นี่ทำงานได้ดี เพียงแค่ import โมดูลในไฟล์ APP.MODULE.TS
import {MatNativeDateModule} from '@angular/material/core';
imports: [
...
MatNativeDateModule
],
คำตอบ #2
ฉันแก้ปัญหานี้โดยการเพิ่มโมดูลต่อไปนี้:
import {MatNativeDateModule} from '@angular/material';
import { MatMomentDateModule } from "@angular/material-moment-adapter";
และในการนำเข้าของคุณ
imports: [
...
MatDatepickerModule,
MatButtonModule,
MatFormFieldModule,
MatNativeDateModule, MatMomentDateModule,
],
ฉันไม่ได้ลึกซึ้งเกี่ยวกับ material-angular ขนาดนั้น ฉันแค่ทำตามคำแนะนำจากข้อความแสดงข้อผิดพลาด
ข้อผิดพลาด Error: MatDatepicker: ไม่พบตัวจัดหาสำหรับ DateAdapter คุณต้องนำเข้าหนึ่งในโมดูลต่อไปนี้ที่รากแอปพลิเคชันของคุณ: MatNativeDateModule, MatMomentDateModule หรือจัดหาการใช้งานแบบกำหนดเอง
คำเตือน: สำหรับเวอร์ชัน Angular ใหม่ๆ คุณเพียงแค่ต้องนำเข้าโมดูลเดียว:
import { MatNativeDateModule } from '@angular/material/core';
...
imports: [
...
MatDatepickerModule,
MatButtonModule,
MatFormFieldModule,
MatNativeDateModule,
],
แทนที่จะ
import {MatNativeDateModule} from '@angular/material';
import { MatMomentDateModule } from "@angular/material-moment-adapter";
...
imports: [
...
MatDatepickerModule,
MatButtonModule,
MatFormFieldModule,
MatNativeDateModule, MatMomentDateModule,
],
คำตอบ #3
ฉันได้แล้ว ที่นี่ทำงานได้ดี เพียงแค่ import โมดูลในไฟล์ 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>
