关于Angular中的typescript:html2canvas-无法调用类型缺少调用签名的表达式

html2canvas in Angular - Cannot invoke an expression whose type lacks a call signature

我正在尝试将html2canvas库用于Angular 8项目。

我还尝试通过npm install --save @types/html2canvas在我的项目中安装html2canvas类型,但仍然无法正常工作。

模板:

1
2
3
  <form>
    ...
  </form>

零件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import * as html2canvas from 'html2canvas';

@Component({
  selector: 'app-pdf-viewer',
  templateUrl: './pdf-viewer.component.html',
  styleUrls: ['./pdf-viewer.component.scss']
})
export class PdfViewerComponent {
  @ViewChild('myform', { static: false, read: ElementRef }) pdfForm: ElementRef;

  constructor() {}


  pdfDownload() {
    html2canvas(this.pdfForm.nativeElement).then(canvas => {
      const imgData = canvas.toDataURL('image/png');
      document.body.appendChild(canvas);
    });
  }

}

我的意图是将表单呈现为画布,但是应用程序引发错误:

src / app / grid / pdf-viewer / pdf-viewer.component.ts中的错误(19,5):错误TS2349:无法调用类型缺少调用签名的表达式。 输入'typeof import(" myroute")',表示您不支持呼叫签名。


解决了! 谢谢 :)

最后,我以这种方式导入:

1
import html2canvas from 'html2canvas';

对于Angular 8,请参阅以下链接:https://github.com/niklasvh/html2canvas/issues/1896#issuecomment-506129710

基本上,您需要安装html2canvas的1.0.0-rc.1版本才能运行。


在Angular 8上也有同样的问题,我能够无错误导入库的唯一方法是使用require。

看到这篇文章。