Angular 9模块存在jsPDF问题(已安装的类型+软件包本身)
当执行ng serve时,它在执行ng build时工作--prod,它有错误
ERROR in src/app/xxx/xxxx.componentomponent.ts:52:27 - error TS2351: This expression is not constructable.
Type 'typeof jsPDF' has no construct signatures.
52 let pdf = new jsPDF('p', 'mm', 'a4');
~~~~~
src/app/xxx/xxxx.component.ts:7:1
7 import * as jsPDF from 'jspdf';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.tsconfig文件有"esModuleInterop":true,
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"importHelpers": true,
"target": "es2015",
"allowSyntheticDefaultImports":true,
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}我像这样导入模块:
**import * as jsPDF from 'jspdf';**在我的类中这样使用它:
generatePDF() {
var data = document.getElementById('contentToConvert');
html2canvas(data).then(canvas => {
var imgWidth = 208;
var imgHeight = canvas.height * imgWidth / canvas.width;
const contentDataURL = canvas.toDataURL('image/png')
let pdf = **new jsPDF**('p', 'mm', 'a4');
var position = 0;
pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
pdf.save('skill-set.pdf');
});
}我还尝试在angular.json的脚本部分添加模块js文件
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js",
**"node_modules/jspdf/dist/jspdf.min.js"**
]发布于 2020-08-29 21:51:55
(在angular 10中测试过,所以我猜这个修复是针对更新的angular版本的)
而不是这样:
import * as jsPDF from 'jspdf';写下这个:
import jspdf from 'jspdf';当心!第二行中的两个 jspdf 都是小写字母
然后你的
let pdf = new jsPDF('p', 'mm', 'a4'); 也可以,只需将全部更改为小写字母(在执行此之前,您必须像我的第二个导入行那样以小写字母导入)
let pdf = new jspdf('p', 'mm', 'a4');发布于 2020-06-28 06:44:24
我设置了一个全新的angular 10项目,并且能够使用jspdf。
> npx @angular/cli new pdf-viewer --strict --defaults true安装jspdf包
> cd pdf-viewer
> npm install jspdf --save
> npm install @types/jspdf --save-dev

tsconfig.base.json。在compilerOptions中添加allowSyntheticDefaultImports
"allowSyntheticDefaultImports": true在src/app/app.component.ts中添加一些要测试的
import { Component, ChangeDetectionStrategy } from '@angular/core';
import jsPDF from 'jspdf';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
title = 'pdf-viewer';
constructor() {
this.generatePDF();
}
private generatePDF(): void {
const doc = new jsPDF();
doc.text('Hello world!', 10, 10);
doc.save('a4.pdf');
}
}http://localhost:4200。你会注意到,只要我们打开网页,a4.pdf就会被下载。打开PDF文件以验证文件的完整性。> npm start发布于 2020-11-06 21:53:29
而不是这样:
import * as jsPDF from 'jspdf';写下这个:
import JSPDF from 'jspdf';然后你的
let pdf = new JSPDF.jsPDF(); https://stackoverflow.com/questions/62609378
复制相似问题