问题描述
尝试通过带有moment.js的rangePicker选项实现Angular Material v10 datepicker但是,当我结合使用矩和rangePicker时,就会出现此错误.
Trying to implement Angular Material v10 datepicker with the rangePicker option with moment.jsBut when I use moment in combination with the rangePicker it gives me this error.
错误:date.getFullYear不是函数
当我尝试选择第二个日期 matEndDate
This error happens when I try to select the second date matEndDate
修改后的Google Stackblitz示例显示了错误:
Modified Google Stackblitz example showing the error:
https://stackblitz.com/edit/angular-imb7gg?file=src/app/datepicker-moment-example.ts
推荐答案
范围日期选择器使用范围策略,错误的 DateAdapter
.
Range datepicker uses range strategy with wrong DateAdapter
.
为了修复它,您应该在提供自定义 DateAdapter
的同一级别上提供 MAT_DATE_RANGE_SELECTION_STRATEGY
令牌:
In order to fix it you should provide MAT_DATE_RANGE_SELECTION_STRATEGY
token on the same level where you provided custom DateAdapter
:
import {
MAT_DATE_RANGE_SELECTION_STRATEGY,
DefaultMatCalendarRangeStrategy
} from '@angular/material/datepicker';
...
providers: [
{ provide: MAT_DATE_RANGE_SELECTION_STRATEGY, useClass: DefaultMatCalendarRangeStrategy},
{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
....
确保 DefaultMatCalendarRangeStrategy
类将MomentDateAdapter作为 DateAdapter
实现而不是 NativeDateAdapter
It makes sure that DefaultMatCalendarRangeStrategy
class will get MomentDateAdapter as a DateAdapter
implementation instead of NativeDateAdapter
Forked Stackblitz
这篇关于Angular Material 10范围datepicker和moment.js错误:date.getFullYear不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!