Angular Material 6 中的 CustomeDateAdapter [英] CustomeDateAdapter in Angular Material 6

查看:23
本文介绍了Angular Material 6 中的 CustomeDateAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个 CustomDateAdapter,如下所述:https://medium.com/@esanjiv/working-with-custom-dateadapter-for-angular-material-2-datepicker-76d4446277dc

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

现在,我正在执行 Angular Material 6 迁移,但出现以下错误

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 第 792 行是这样的:

core.es5.js line 792 is this:

_this.useUtcForDisplay = !platform.TRIDENT;

似乎平台未定义.

我注意到在 Material 6 中 NativeDateAdapter 的构造函数发生了变化,它也需要一个平台作为输入.

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);

我错过了什么?如何在 Angular Material 6 中实现 CustomDateAdapter?

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

推荐答案

我遇到了同样的问题.这是我的修复.

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 {
}

并且您还没有覆盖 YourCustomDateAdapter 的构造函数.

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

这篇关于Angular Material 6 中的 CustomeDateAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆