You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular项目安装@angular-material-components/datetime-picker遇依赖冲突

解决Angular项目中@angular-material-components/datetime-picker的依赖冲突问题

问题背景

我正在开发一个使用Angular-Material的Angular应用,想要在表单字段中使用matdatetimepicker。按照指引执行安装命令:

npm install @angular-material-components/datetime-picker

但npm抛出依赖树解析错误:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: deywuro-app@0.0.0
npm ERR! Found: @angular/cdk@13.3.9
npm ERR! node_modules/@angular/cdk
npm ERR!   @angular/cdk@"^13.0.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/cdk@"^15.0.1" from @angular-material-components/datetime-picker@9.0.0
npm ERR! node_modules/@angular-material-components/datetime-picker
npm ERR!   @angular-material-components/datetime-picker@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

查阅文档后,尝试安装兼容@angular/cdk@13.x的7.0版本:

npm install @angular-material-components/datetime-picker@7.0

但依然报错,因为该版本要求@angular/common@^13.0.1,而项目中@angular/common是14.2.12:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: deywuro-app@0.0.0        
npm ERR! Found: @angular/common@14.2.12
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"^14.2.0" from the root project
npm ERR!   peer @angular/common@"^13.0.0 || ^14.0.0-0" from @angular/cdk@13.3.9
npm ERR!   node_modules/@angular/cdk
npm ERR!     @angular/cdk@"^13.0.0" from the root project
npm ERR!     peer @angular/cdk@"^13.0.1" from @angular-material-components/datetime-picker@7.0.1
npm ERR!     node_modules/@angular-material-components/datetime-picker
npm ERR!       @angular-material-components/datetime-picker@"7.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@"^13.0.1" from @angular-material-components/datetime-picker@7.0.1 
npm ERR! node_modules/@angular-material-components/datetime-picker
npm ERR!   @angular-material-components/datetime-picker@"7.0" from the root project

项目依赖情况(package.json):

{
  "name": "deywuro-app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^14.2.0",
    "@angular/cdk": "^13.0.0",
    "@angular/common": "^14.2.0",
    "@angular/compiler": "^14.2.0",
    "@angular/core": "^14.2.0",
    "@angular/forms": "^14.2.0",
    "@angular/material": "^13.0.0",
    "@angular/platform-browser": "^14.2.0",
    "@angular/platform-browser-dynamic": "^14.2.0",
    "@angular/router": "^14.2.0",
    "@fortawesome/fontawesome-free": "^6.2.1",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.2.7",
    "@angular/cli": "~14.2.7",
    "@angular/compiler-cli": "^14.2.0",
    "@types/jasmine": "~4.0.0",
    "jasmine-core": "~4.3.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.0.0",
    "typescript": "~4.7.2"
  }
}

解决方案

方案1:统一Angular CDK和Material版本到14.x(推荐)

项目核心Angular包是14.2.x,但CDK和Material停留在13.x,这本身就是潜在兼容性问题。先升级这两个包到匹配14.x的版本:

npm install @angular/cdk@^14.2.0 @angular/material@^14.2.0

升级完成后,安装兼容Angular 14的@angular-material-components/datetime-picker版本(8.x版本适配Angular 14):

npm install @angular-material-components/datetime-picker@^8.0.0

方案2:安装兼容Angular 14和CDK 13的datetime-picker版本

如果暂时不想升级CDK和Material,可以选择7.1.x版本,该版本开始支持Angular 14同时兼容CDK 13:

npm install @angular-material-components/datetime-picker@^7.1.0

方案3:临时使用--legacy-peer-deps跳过冲突(不推荐生产环境)

若仅需快速验证功能,可通过--legacy-peer-deps参数忽略peer dependency冲突:

npm install @angular-material-components/datetime-picker@7.0 --legacy-peer-deps

此方式可能引发运行时兼容性问题,仅建议用于开发测试阶段。


内容的提问来源于stack exchange,提问作者JOSEPH SENIOR ABROKWAH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 13:30:15