ERROR: Dependency @types/html2canvas must be explicitly allowed using the "allowedNonPeerDependencies" option

ERROR: Dependency @types/html2canvas must be explicitly allowed using the "allowedNonPeerDependencies" option

我已经在我的 angular 库项目中安装了 html2canvas,当我在生产模式下编译时(运行 ng build --prod 命令),我收到以下错误:

ERROR: Dependency @types/html2canvas must be explicitly allowed using the "allowedNonPeerDependencies" option.

我该如何解决?

您可以将该库添加到您的 peerDependencies in package.json. I strongly recommend using the peerDependencies 策略中,因为它会向其他人显式公开您的库依赖于其他库的信息:

{
    ...
    "scripts": {...},
    "peerDependencies": {
        ...
        "@types/html2canvas": "0.0.36",
        ...
    },
}

或者您可以使用 ng-package.json 上的选项:

{
    ...
    "lib": {
        "entryFile": "src/public-api.ts"
    },
    "allowedNonPeerDependencies": [
        "@type/html2canvas"
    ]
    ...
}