我正在尝试在我的Angular 5.2.0项目中使用jsPDF和jspdf-autotable。 我的package.json如下(相关部分):
"dependencies": {
...
"jspdf": "^1.3.5",
"jspdf-autotable": "^2.3.2"
...
}
以下是我的angular-cli.json文件(相关部分):
"scripts": [
...
"../node_modules/jspdf/dist/jspdf.min.js",
"../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.js"
...
]
我的组件(相关部分):
import * as jsPDF from 'jspdf';
import 'jspdf-autotable';
@Component({
selector: "energy-login",
templateUrl: "./login.component.html",
styleUrls: ["./login.component.scss"]
})
export class MyComponent implements OnInit {
constructor() {}
ngOnInit() {}
downloadPDF() {
let columns = ["ID", "Name", "Country"];
let rows = [
[1, "Shaw", "Tanzania"],
[2, "Nelson", "Kazakhstan"],
[3, "Garcia", "Madagascar"],
];
let doc = new jsPDF('l', 'pt');
doc.autoTable(columns, rows); // typescript compile time error
doc.save('table.pdf');
}
}
它说:
[ts] Property 'autoTable' does not exist on type 'jsPDF'.
我尝试使用以下代码替换我的组件中的导入:
// import * as jsPDF from 'jspdf';
// import 'jspdf-autotable';
declare var jsPDF: any;
如果没有编译时错误,但在运行downloadPDF()函数时出现以下提示:
ERROR ReferenceError: jsPDF is not defined