我有此下载功能,正在对“ downloadCallback”进行回调。
我希望在download函数本身内部定义downloadCallback函数。我该怎么办?
这是download()函数:
download() {
let blob = this.diagram.makeImageData({ background: "white", returnType: "blob", callback: <any>this.downloadCallback
})
};
这是downloadCallback函数:
downloadCallback(blob : any) : void {
var url = window.URL.createObjectURL(blob);
var reader = new FileReader();
reader.onloadend = () => {
var imageDataUrl = reader.result;
var imgWidth = imageDataUrl.width;
var imgHeight = imageDataUrl.height;
let pdf = new jspdf('p', 'mm', 'a4');
var position = 0;
pdf.setFontSize(30);
pdf.text("Diagram", 100, 20, 'center');
pdf.addImage(imageDataUrl, 'PNG', 0, 25, position, imgWidth, imgHeight);
pdf.save('Diagram.pdf');
console.log(imageDataUrl);
};
reader.readAsDataURL(blob);
}
我正在使用GoJS。我想做的是下载PDF格式的图形。
答案 0 :(得分:1)
确保在尝试使用函数之前先定义函数,如下所示:
download() {
// Create the function
const downloadCallback = (): void => {
console.log("Test");
}
// Use it
let blob = this.diagram.makeImageData({ background: "white", returnType: "blob", callback: <any>downloadCallback});
}
答案 1 :(得分:0)
您只需将downloadCallback()放在download()函数中,并将其从函数调用中删除:
download() {
let blob = this.diagram.makeImageData({ background: "white", returnType: "blob", callback: <any>downloadCallback
});
downloadCallback(blob : any) : void {
// ...
}
};
应该没有问题,但是我没有测试