如何解决Angular- jsPDF警告,意外的tocken和jsPDF.fromHTML不是函数
我想在Angular中打印html代码和jsPDF,但我不能。我需要帮助,看看。
ts.config.app.json
{
"extends": "./tsconfig.base.json","compilerOptions": {
"outDir": "./out-tsc/app","types": [],"allowSyntheticDefaultImports": true
},"files": [
"src/main.ts","src/polyfills.ts"
],"include": [
"src/**/*.d.ts"
]
}
npm jspdf模块
angular.json脚本
"scripts": [
"node_modules/jquery/dist/jquery.js","node_modules/popper.js/dist/umd/popper.js","node_modules/bootstrap/dist/js/bootstrap.js","node_modules/jspdf/dist/jspdf.es.min.js"
]
如果在课堂上我像这样import jsPDF from 'jspdf';
或这样import { jsPDF } from "jspdf";
导入,我会收到此警告和此错误。
警告:
WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:141-151
Critical dependency: the request of a dependency is an expression
WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:240-254
Critical dependency: the request of a dependency is an expression
错误:
scripts.js:18390 Uncaught SyntaxError: Unexpected token 'export'
如果我像这样import * as jsPDF from "jspdf";
导入jspdf,则会收到此错误:
错误:
Type 'typeof import("jspdf")' has no construct signatures.
const doc = new jsPDF('p','pt','letter');
这是我的课程:
//PDF
// import { jsPDF } from "jspdf";
import jsPDF from 'jspdf';
// import * as jsPDF from "jspdf";
@Component({
selector: 'app-cash-register',templateUrl: './cash-register.component.html'
})
export class CashRegisterComponent implements OnInit {
@ViewChild("ticket") ticket: ElementRef;
public printPDF(): void {
var pdf = this.ticket.nativeElement.innerHTML;
const doc = new jsPDF('p','letter');
const margins = {
top: 80,bottom: 60,left: 40,width: 522
};
doc.html(pdf,{
callback: function(doc) {
doc.save("Test1.pdf");
}
})
// or
doc.fromHTML(pdf,margins.left,margins.top,{},function () {
doc.save("Test2.pdf");
},margins);
}
}
并且如果我没有在angular.json中导入jspdf,则在尝试下载时会出现此错误:
core.js:4197 ERROR TypeError: doc.fromHTML is not a function
解决方法
我没有一个好的解决方案,但这就是我使它起作用的方式。看起来jspdf 2.0.0抛出“ doc.fromHTML不是函数”。角度为10(我不确定它是否适用于以前的角度版本。)
我使用删除了jsdpf 2.0.0
me$ = this.store.select((s) => s.user.firstName);
然后我安装了以前的版本:
npm uninstall jspdf
工作示例:
npm install jspdf@1.5.3
,
doc.fromHTML()
问题也发生在Angular 7.x上。如果降级为jsPDF 1.5.3,则可以正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。