【发布时间】:2023-09-20 14:21:01
【问题描述】:
我创建了一个生成 JSON 数据的 Angular2 应用程序。我想将此 JSON 输出保存到文件中,最好是 PDF 文件。此应用程序是使用 Typescript 编写的。
我使用 jsPDF 将 JSON 数据写入 PDF 文件。我已经使用npm install jspdf --save 通过 npm 安装了 jsPDF 包。我还在 index.html 页面中添加了必要的链接。我在应用程序运行时对其进行了这些更改。我能够将 JSON 数据保存到文件中。
当我停止并重新启动应用程序时,它没有按预期启动。我收到以下错误:
json-excel@1.0.0 start C:\Users*****\Documents\Projects\Angular\json-to-pdf
tsc && concurrently "npm run tsc:w" "npm run lite"
app/components/app.component.ts(35,19):错误 TS2304:找不到名称“jsPDF”。
我正在添加我使用过的代码。
package.json:
"dependencies": {
"@angular/common": "2.0.0-rc.1",
"@angular/compiler": "2.0.0-rc.1",
"@angular/core": "2.0.0-rc.1",
"@angular/http": "2.0.0-rc.1",
"@angular/platform-browser": "2.0.0-rc.1",
"@angular/platform-browser-dynamic": "2.0.0-rc.1",
"@angular/router": "2.0.0-rc.1",
"@angular/router-deprecated": "2.0.0-rc.1",
"@angular/upgrade": "2.0.0-rc.1",
"angular2-in-memory-web-api": "0.0.7",
"bootstrap": "^3.3.6",
"es6-shim": "^0.35.0",
"jquery": "^2.2.4",
"jspdf": "^1.2.61",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"systemjs": "0.19.27",
"zone.js": "^0.6.12"
}
index.html:
<html>
<head>
<title>JSON to PDF</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
....
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
....
</head>
<!-- 3. Display the application -->
<body class="bg">
<div>
<app>Loading...</app>
</div>
</body>
</html>
app.component.ts:
import {Component} from '@angular/core';
@Component({
selector: 'app',
template: `<button (click)="createFile()">Export JSON to PDF</button>`
})
export class AppComponent {
public item = {
"Name" : "XYZ",
"Age" : "22",
"Gender" : "Male"
}
constructor() {}
createFile(){
var doc = new jsPDF();
var i=0;
for(var key in this.item){
doc.text(20, 10 + i, key + ": " + this.item[key]);
i+=10;
}
doc.save('Test.pdf');
}
}
我认为 app.component.ts 文件中缺少一些导入语句。如果缺少什么,有人可以指出正确的导入语句吗?如果这是我犯的错误,我可以知道如何在 Angular2 中正确使用 jsPDF 吗?
谢谢。
【问题讨论】:
-
面临同样的问题..找人回答!!!
-
你找到答案了吗??我被这个错误困了很长时间:(
标签: json pdf typescript angular jspdf