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

标签 angular jspdf

我正在尝试在我的 Angular 7.1.3 项目中使用 jsPDF。

我的 package.json 如下(相关部分):

"dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "core-js": "^2.5.4",
    "jspdf": "^1.5.3",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },

我的组件:

import { Component, ViewChild, ElementRef } from '@angular/core';
import { jsPDF } from 'jspdf';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular-html-pdf';

  @ViewChild('pdfTable') pdfTable: ElementRef;


  public downloadAsPDF() {
    const doc = new jsPDF();

    const specialElementHandlers = {
      '#editor': function (element, renderer) {
        return true;
      }
    };

    const pdfTable = this.pdfTable.nativeElement;

    doc.fromHTML(pdfTable.innerHTML, 15, 15, {
      width: 190,
      'elementHandlers': specialElementHandlers
    });

    doc.save('tableToPdf.pdf');
  }
}

没有编译时错误,但运行 downloadAsPDF() 函数给出:

错误类型错误:“jspdf__WEBPACK_IMPORTED_MODULE_2__.jsPDF 不是构造函数”

最佳答案

错误是导入语句引起的

从'jspdf'导入{jsPDF};

更改为:

从 'jspdf' 导入 jsPDF;

关于angular - 如何在 Angular 7 中使用 jsPDF?获取jspdf__WEBPACK_IMPORTED_MODULE_2__.jsPDF is not a constructor错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809502/

相关文章:

javascript - 如何在单击按钮时显示验证消息?

swift - WKWebView 中无法下载 PDF

javascript - 使用单选按钮和复选框从 HTML 生成 PDF

Angular 4 @HostBinding 绑定(bind)到 style 属性

angular - 从 Angular 2 中的组件代码访问模板中异步管道的结果

angular - 在 angular 中找不到项目名称

jspdf - 如何使用 jsPDF 获取 PDF 中的表格行图像?

javascript - 使用 React 生成动态 PDF

javascript - jsPDF 类型不存在属性 'autoTable'

typescript - 在 Angular2 rc1 中基于路由隐藏元素