CustomeDateAdapter in Angular Material 6

怎甘沉沦 提交于 2019-12-12 11:46:27

问题


I have implemented a CustomDateAdapter as described here: https://medium.com/@esanjiv/working-with-custom-dateadapter-for-angular-material-2-datepicker-76d4446277dc

Now, I am doing the Angular Material 6 migration and I get the following error

core.js:1598 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'TRIDENT' of undefined
TypeError: Cannot read property 'TRIDENT' of undefined
at CustomDateAdapter.NativeDateAdapter [as constructor] (core.es5.js:792)
at new CustomDateAdapter (custom-date-adapter.ts:21)
at _createClass (core.js:9262)
...

core.es5.js line 792 is this:

_this.useUtcForDisplay = !platform.TRIDENT;

It seems that the platform is undefined.

I noticed that the constructor of the NativeDateAdapter has changed in Material 6, it also requires a platform as input.

constructor(matDateLocale: string, platform: Platform);

What I am missing? How can I implement a CustomDateAdapter in Angular Material 6?


回答1:


I had the same problem. Here is my fix.

// in your shared or main.module.ts
// … other imports
import {Platform, PlatformModule} from '@angular/cdk/platform';

@NgModule({
  imports: [
    // …
    PlatformModule
  ],
  exports: [
    // …
    PlatformModule
  ],
  providers: [
    {provide: DateAdapter, useClass: YourCustomDateAdapter, deps: [MAT_DATE_LOCALE, Platform]},
  ]
 });
 export class SharedModule {
}

And you haven't override the YourCustomDateAdapter's constructor.




回答2:


I solved this problem by instantiating in my code a Platform object

export class CustomDateAdapter extends NativeDateAdapter {

  constructor(matDateLocale: string) {
    super(matDateLocale, new Platform());
  }

...

}

Another option is to provide it with a factory

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    {
      provide: Platform,
      useFactory: platformProviderFactory,
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

export function platformProviderFactory() {
  return () => new Platform();
}


来源:https://stackoverflow.com/questions/50608655/customedateadapter-in-angular-material-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!