我创建了一个Angular2应用程序,用于生成JSON数据。我希望将这个JSON输出保存在一个文件中,最好是PDF文件。这个应用程序是使用TypeScript编写的。
我使用了jsPDF将JSON数据写入PDF文件。我通过npm安装了jsPDF包,使用命令
但是,当我停止并重新启动该应用程序时,它无法按预期启动,我得到了以下错误信息:
我使用了jsPDF将JSON数据写入PDF文件。我通过npm安装了jsPDF包,使用命令
npm install jspdf --save
。我还在index.html页面添加了必要的链接。当应用程序正在运行时,我对应用程序进行了这些更改,并成功地将JSON数据保存到了文件中。但是,当我停止并重新启动该应用程序时,它无法按预期启动,我得到了以下错误信息:
下面是我使用的代码: package.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): error TS2304: Cannot find name 'jsPDF'.
"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?
谢谢。