角2 - 导入html2canvas

角2 - 导入html2canvas

问题描述:

我已经用我的npm install html2canvas --save项目angular 2安装html2canvas。如果我现在去的任何文件和写import * as html2canvas from 'html2canvas'它给人的错误:角2 - 导入html2canvas

Cannot find module 'html2canvas' 

我的包文件看起来是这样的:

{ 
    ... 
    "scripts": { 
    ... 
    }, 
    "dependencies": { 
    ... 
    "html2canvas": "^0.5.0-beta4", 
    ... 
    }, 
    "devDependencies": { 
    ... 
    } 
} 

上我试图导入html2canvas的文件是:

import { Injectable } from '@angular/core'; 
import * as jsPDF from 'jspdf'; 
import * as html2canvas from 'html2canvas'; 

@Injectable() 
export class pdfGeneratorService { 

    ... 
} 
+2

您需要安装分型的这一点。 – echonax

+0

啊,就是这样。如果你可以做出答案,我会把它作为答案。 – FlorisdG

由于Angular2使用打字稿,您需要安装该模块的打字稿定义文件。

可以从@types安装(如果存在的话)。如果没有,您可以创建自己的定义文件并将其包含在项目中。

此外,onrendered选项回调函数可能无法正常工作。相反,你可以使用“然后”,如下:

html2canvas(document.body).then((canvas) => { 
    document.body.appendChild(canvas); 
}); 

https://stackoverflow.com/a/45366038/3119507