在定义本身中定义回调函数
Defining a callback function in the definition itself
我有这个下载功能,它正在回调 "downloadCallback"。
我希望在下载函数本身内部定义 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 格式的图表。
您可以将 downloadCallback() 放在 download() 函数中并将其从函数调用中删除:
download() {
let blob = this.diagram.makeImageData({ background: "white", returnType: "blob", callback: <any>downloadCallback
});
downloadCallback(blob : any) : void {
// ...
}
};
应该没有问题,不过我没有测试
确保在尝试使用之前定义函数,如下所示:
download() {
// Create the function
const downloadCallback = (): void => {
console.log("Test");
}
// Use it
let blob = this.diagram.makeImageData({ background: "white", returnType: "blob", callback: <any>downloadCallback});
}
我有这个下载功能,它正在回调 "downloadCallback"。
我希望在下载函数本身内部定义 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 格式的图表。
您可以将 downloadCallback() 放在 download() 函数中并将其从函数调用中删除:
download() {
let blob = this.diagram.makeImageData({ background: "white", returnType: "blob", callback: <any>downloadCallback
});
downloadCallback(blob : any) : void {
// ...
}
};
应该没有问题,不过我没有测试
确保在尝试使用之前定义函数,如下所示:
download() {
// Create the function
const downloadCallback = (): void => {
console.log("Test");
}
// Use it
let blob = this.diagram.makeImageData({ background: "white", returnType: "blob", callback: <any>downloadCallback});
}