Vla*_*nko 4 jspdf typescript angular angular9 angular-cli-v9
有问题 jsPDF 的 Angular 9 模块(已安装的类型 + 包本身)
当做 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.
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样导入模块:
**import * as jsPDF from 'jspdf';**
Run Code Online (Sandbox Code Playgroud)
在我的课堂上像这样使用它:
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');
});
}
Run Code Online (Sandbox Code Playgroud)
我还尝试在 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"**
]
Run Code Online (Sandbox Code Playgroud)
小智 14
(在 angular 10 中测试,所以我猜这个修复是针对较新的 angular 版本)
而不是这个:
import * as jsPDF from 'jspdf';
Run Code Online (Sandbox Code Playgroud)
写这个:
import jspdf from 'jspdf';
Run Code Online (Sandbox Code Playgroud)
[当心!双方jspdf在第二行是小写字母]
那么你的
let pdf = new jsPDF('p', 'mm', 'a4');
Run Code Online (Sandbox Code Playgroud)
也可以,只需将所有内容更改为小写字母(在执行此操作之前,您必须像我的第二个导入行一样导入小写字母)
let pdf = new jspdf('p', 'mm', 'a4');
Run Code Online (Sandbox Code Playgroud)
我设置了一个新的 angular 10 项目并且能够使用jspdf.
> npx @angular/cli new pdf-viewer --strict --defaults true
Run Code Online (Sandbox Code Playgroud)
jspdf从 npm 注册表安装包> cd pdf-viewer
> npm install jspdf --save
> npm install @types/jspdf --save-dev
Run Code Online (Sandbox Code Playgroud)
tsconfig.base.json。添加allowSyntheticDefaultImports里面compilerOptions"allowSyntheticDefaultImports": true
Run Code Online (Sandbox Code Playgroud)
jspdf代码进行测试src/app/app.component.ts"allowSyntheticDefaultImports": true
Run Code Online (Sandbox Code Playgroud)
http://localhost:4200. a4.pdf一旦我们打开网页,您就会注意到它会被下载。打开 PDF 文件以验证文件的完整性。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');
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10357 次 |
| 最近记录: |