MatDatepicker:找不到DateAdapter的提供程序。您必须在应用程序根目录下导入以下模块之一:MatNativeDateModule

时间:2019-06-08 06:08:14

标签: javascript angular typescript

我已经在我的app.module.ts文件中导入了matdatepicker,但仍然显示一些错误。我无法使材料组件正常工作。该按钮可以正常工作,并使用有角度的材质成分。但是,当我使用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)

6 个答案:

答案 0 :(得分:27)

我明白了,这里工作正常,只需将模块导入APP.MODULE.TS文件中

import {MatNativeDateModule} from '@angular/material/core';
imports: [

...

MatNativeDateModule 
],

答案 1 :(得分:3)

导入已更改。改用这个:

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

答案 2 :(得分:1)

我知道了,这里工作正常 只需将模块导入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>

答案 3 :(得分:0)

我通过添加以下模块解决了该问题:

import {MatNativeDateModule} from '@angular/material';
import { MatMomentDateModule } from "@angular/material-moment-adapter";

以及您的进口商品

imports: [
    ...
    MatDatepickerModule,
    MatButtonModule,
    MatFormFieldModule,
    MatNativeDateModule, MatMomentDateModule,
  ],

我对材质的了解并不深,我只遵循错误消息建议

  

错误错误:MatDatepicker:未找到DateAdapter提供程序。您   必须在应用程序根目录下导入以下模块之一:   MatNativeDateModule,MatMomentDateModule或提供自定义   实施。

答案 4 :(得分:0)

我刚刚使用了第一个导入:

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

另一个导入使我遇到了另一个我没有解决的错误,但这不是必需的。

别忘了将MatNativeDateModule添加到app.module.ts中的imports数组中。

答案 5 :(得分:0)

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

这对我来说很好用