带有 jsPDF 模块 : Type 'typeof jsPDF' has no construct signatures 的 Angular 9

标签 angular typescript jspdf angular9 angular-cli-v9

Angular 9
有问题 jsPDF 的模块(安装的类型 + 包本身)
当做 ng serve 它工作
执行 ng build --prod 时,出现错误

ERROR in src/app/xxx/xxxx.componentomponent.ts:52:27 - error TS2351: This expression is not constructable.
  Type 'typeof jsPDF' has no construct signatures.

52       let pdf = new jsPDF('p', 'mm', 'a4');
                             ~~~~~

  src/app/xxx/xxxx.component.ts:7:1
    7 import * as jsPDF from 'jspdf';
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.
tsconfig 文件有 "esModuleInterop": true,
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "importHelpers": true,
    "target": "es2015",
    "allowSyntheticDefaultImports":true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
我像这样导入模块:
**import * as jsPDF from 'jspdf';**
在我的类里面像这样使用它:
 generatePDF() {
    var data = document.getElementById('contentToConvert');
    html2canvas(data).then(canvas => {
      var imgWidth = 208;
      var imgHeight = canvas.height * imgWidth / canvas.width;
      const contentDataURL = canvas.toDataURL('image/png')
      let pdf = **new jsPDF**('p', 'mm', 'a4');
      var position = 0;
      pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
      pdf.save('skill-set.pdf');
    });
  }
我还尝试在 angular.json 的脚本部分添加模块 js 文件
  "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js",
              **"node_modules/jspdf/dist/jspdf.min.js"**
            ]

最佳答案

(在 Angular 10 中测试,所以我猜这个修复程序适用于较新的 Angular 版本)
而不是这个:

import * as jsPDF from 'jspdf';
写这个:
import jspdf from 'jspdf';
[ 小心 !两个jspdf第 2 行是小写字母 ]
那么你的
let pdf = new jsPDF('p', 'mm', 'a4'); 
也可以,只需将全部更改为小写字母( 在执行此操作之前,您必须像我的第二个导入行一样以小写字母导入 )
let pdf = new jspdf('p', 'mm', 'a4');

关于带有 jsPDF 模块 : Type 'typeof jsPDF' has no construct signatures 的 Angular 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62609378/

相关文章:

angular - 如何在 Angular 7 中使用 jsPDF?获取jspdf__WEBPACK_IMPORTED_MODULE_2__.jsPDF is not a constructor错误

angular - karma 测试 : Random Test fails when using async in other tests

javascript - ngClass 条件不适用于多种条件

angular - 无法使用 img ng-src(无法绑定(bind)到 ng-src,因为它不是 img 的已知属性)

javascript - Angular 5 : Build json from values

Typescript:转换所有函数参数的通用函数类型

javascript - Angular 8 - 如何将数据从 1 个组件导出到 2,然后再导出到 3

node.js - npm 错误! azure devops 中的代码 ELIFECYCLE 无法使其运行

jspdf - 生成的 PDF 中的自动换行(使用 jsPDF)?

javascript - jsPDF低质量图像导出