Angular日期选择器引入moment后模块找不到及导入异常求助
Angular日期选择器Moment导入错误修复方案
问题描述
按照Angular日期选择器官方文档代码开发时,遭遇Moment导入相关错误。安装moment包后问题未解决,反而引发更多原有导入的错误。已尝试执行npm install --save @angular/core和npm install --save命令,错误仍存在。
相关代码
TypeScript代码
import {Component, ViewEncapsulation} from '@angular/core'; import {FormControl} from '@angular/forms'; import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter'; import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core'; import {MatDatepicker} from '@angular/material/datepicker'; // Depending on whether rollup is used, moment needs to be imported differently. // Since Moment.js doesn't have a default export, we normally need to import using the `* as` // syntax. However, rollup creates a synthetic default module and we thus need to import it using // the `default as` syntax. import * as _moment from 'moment'; // tslint:disable-next-line:no-duplicate-imports import {default as _rollupMoment, Moment} from 'moment'; const moment = _rollupMoment || _moment; // See the Moment.js docs for the meaning of these formats: // https://momentjs.com/docs/#/displaying/format/ export const MY_FORMATS = { parse: { dateInput: 'MM/YYYY', }, display: { dateInput: 'MM/YYYY', monthYearLabel: 'MMM YYYY', dateA11yLabel: 'LL', monthYearA11yLabel: 'MMMM YYYY', }, }; /** @title Datepicker emulating a Year and month picker */ @Component({ selector: 'datepicker-views-selection-example', templateUrl: 'datepicker-views-selection-example.html', styleUrls: 'datepicker-views-selection-example.css', providers: [ // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your // application's root module. We provide it at the component level here, due to limitations of // our example generation script. { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS], }, {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS}, ], encapsulation: ViewEncapsulation.None, }) export class DatepickerViewsSelectionExample { date = new FormControl(moment()); setMonthAndYear(normalizedMonthAndYear: Moment, datepicker: MatDatepicker<Moment>) { const ctrlValue = this.date.value!; ctrlValue.month(normalizedMonthAndYear.month()); ctrlValue.year(normalizedMonthAndYear.year()); this.date.setValue(ctrlValue); datepicker.close(); } }
HTML模板
<mat-form-field appearance="fill"> <mat-label>Month and Year</mat-label> <input matInput [matDatepicker]="dp" [formControl]="date"> <mat-hint>MM/YYYY</mat-hint> <mat-datepicker-toggle matIconSuffix [for]="dp"></mat-datepicker-toggle> <mat-datepicker #dp startView="multi-year" (monthSelected)="setMonthAndYear($event, dp)" panelClass="example-month-picker"> </mat-datepicker> </mat-form-field>
错误截图

修复步骤
1. 安装正确的依赖包
确保同时安装moment及其类型定义,适配Angular版本:
npm install moment @types/moment --save
2. 简化Moment导入逻辑
将原有复杂的rollup兼容导入替换为标准导入方式,避免冲突:
// 替换原有moment导入部分 import * as moment from 'moment'; import { Moment } from 'moment';
同时删除const moment = _rollupMoment || _moment;这一行,直接使用导入的moment变量。
3. 配置TypeScript编译选项
在tsconfig.json的compilerOptions中添加以下配置,确保模块导入兼容:
{ "compilerOptions": { "allowSyntheticDefaultImports": true, "esModuleInterop": true, "moduleResolution": "node" } }
4. 清理缓存并重建项目
删除依赖缓存和锁定文件,重新安装后启动项目:
rm -rf node_modules package-lock.json npm install ng serve --open
5. 确认模块导入配置
在使用该组件的NgModule中,确保导入必要的Angular Material和表单模块:
import { MatDatepickerModule } from '@angular/material/datepicker'; import { MatMomentDateModule } from '@angular/material-moment-adapter'; import { ReactiveFormsModule } from '@angular/forms'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; @NgModule({ imports: [ // ...其他模块 ReactiveFormsModule, MatFormFieldModule, MatInputModule, MatDatepickerModule, MatMomentDateModule ] }) export class YourFeatureModule { }
内容的提问来源于stack exchange,提问作者Mega
相关产品推荐
相关产品推荐

