【问题标题】:How to use jspdf-autotable in Angular 4?如何在 Angular 4 中使用 jspdf-autotable?
【发布时间】:2019-10-04 12:15:07
【问题描述】:

我能够导入 jsPDF,但是我不能使用 jspdf-autotable 中的 autoTable 方法,不知道如何将它作为 jsPDF 的依赖项导入。

import { Injectable, Component } from '@angular/core';

import * as jsPDF from 'jspdf';
import { autoTable } from 'jspdf-autotable';

@Injectable()
export class PdfService {

    constructor() {

    }

    convertJsonToPdf(columns: any, jsonData: any) {
       var doc = new jsPDF('p', 'pt');  // OK, created
       doc.autoTable(columns, jsonData); // Fails because autoTable is not in doc
    }
}

【问题讨论】:

  • 尝试将import { autoTable } from 'jspdf-autotable';这一行替换为import 'jspdf-autotable';;
  • @SimonBengtsson 成功了,谢谢。

标签: angular jspdf jspdf-autotable


【解决方案1】:

摘自上面的讨论,替换这一行:

import { autoTable } from 'jspdf-autotable'; 

import 'jspdf-autotable';

【讨论】:

  • angular2 怎么样?要么导入'jspdf-autotable';或从 'jspdf-autotable' 导入 { autoTable };没有任何工作
  • import 'jspdf-autotable'; 应该可以工作。如果对您不起作用,请随意写一个包含更多信息的新问题。
【解决方案2】:

而不是像这样声明:

import * as jsPDF from 'jspdf';
import 'jspdf-autotable';

最好这样做

import 'jspdf';
import 'jspdf-autotable';
declare let jsPDF;

这就是我解决问题的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多