我有一個捕獲 div 的方法。我從另一種方法中呼叫了這個名為 capture() 的方法。
這是代碼:
theimage; // define the variable
callcapture() {
// do stuff
this.capture(); // Call the method here
// Do other stuff below BUT (Do not run the rest of the code until "this.capture() has finished ")
}
capture() {
const element = document.getElementById("capture") as HTMLCanvasElement;
html2canvas(element).then((canvas) => {
this.theimage = canvas.toDataURL();
});
}
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
@viewChild為此使用 Angular 怎么樣?這是rarerly使用本地JS方法是一個好主意getElementById,getElementsByClassName等; 在 Angular 應用程式中。
<canvas #myCanvas></canvas>
@ViewChild('myCanvas', {static: false}) canvasElem: ElementRef;
然后你使用它:
const context = this.canvasElem.nativeElement.getContext("2d");
const base64:string = this.canvasElem.nativeElement.toDataURL();
uj5u.com熱心網友回復:
你可以回傳 promise 并使用另一個then來確保元素已經被捕獲:
callcapture() {
// do stuff
this.capture().then(() => {
// Do other stuff below BUT (Do not run the rest of the code until "this.capture() has finished ")
});
}
capture() {
const element = document.getElementById("capture") as HTMLCanvasElement;
return html2canvas(element).then((canvas) => {
this.theimage = canvas.toDataURL();
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/332663.html
標籤:javascript 有角的 打字稿
上一篇:NullInjectorError:沒有BsDropdownConfig的提供者
下一篇:父級橫向滾動,子級縱向滾動
