错误“库”缺少依赖项 - @angular/material/form-field

时间:2021-05-03 22:52:20

标签: angular angular-material

我有一个已有几年历史的共享组件库项目(只是说它已经运行了一段时间)。我想用 Angular Material 组件替换一些自定义组件。但是,当我将 NgMat 添加到库项目时,尝试在单独的项目中使用它时出现错误。

display: block;

项目 package.json 有:

ERROR in The target entry-point "my library project" has missing dependencies:
- @angular/material/form-field
- @angular/material/datepicker

主 package.json 文件有:

...
    "peerDependencies": {
    "@angular/common": "~10.2.4",
    "@angular/core": "~10.2.4",
    "@angular/forms": "~10.2.4",
    "@angular/material": "^10.2.4",
...

我还在 ... "dependencies": { "@angular/animations": "~10.2.4", "@angular/cdk": "^9.2.4", "@angular/common": "~10.2.4", "@angular/compiler": "~10.2.4", "@angular/core": "~10.2.4", "@angular/forms": "~10.2.4", "@angular/material": "^10.2.4", ... 中将 @angular/material 列入白名单 Dependency @ng-bootstrap/ng-bootstrap must be explicitly whiteliste 中的建议,同时在项目的依赖项中使用 @angular/material 而不是 peerDependencies。

1 个答案:

答案 0 :(得分:0)

@angular/material 在库包的 peerDependencies 中声明。

"peerDependencies": { --> Peer dependencies
    "@angular/common": "~10.2.4",
    "@angular/core": "~10.2.4",
    "@angular/forms": "~10.2.4",
    "@angular/material": "^10.2.4",

这意味着安装库时不会自动安装 peerDependencies 中的包。

要解决问题,

或者您应该将@angular/material 移动到库包中的依赖项。

"dependencies": { --> dependencies
    "@angular/material": "^10.2.4",

(或)在 application package.json 依赖项中声明 @angular/material。

"dependencies": {
    "@angular/animations": "~10.2.4",
    "@angular/cdk": "^9.2.4",
    "@angular/common": "~10.2.4",
    "@angular/compiler": "~10.2.4",
    "@angular/core": "~10.2.4",
    "@angular/forms": "~10.2.4",
    "@angular/material": "^10.2.4" --> Newly added