angular - ประเภท 'Promise' ไม่สามารถกำหนดค่าให้กับประเภท 'string' ได้
ฉันจะใส่ตัวแปรการตอบกลับจากบริการแล้วแสดงมันเมื่อวางเมาส์เหนือได้อย่างไร? ฉันลองทำแบบนี้:
toolTip: string;
async mouseover(params) {
this.handle = setTimeout(() => {
this.toolTip = this.checkSaldo(10, 'FRA');
this.show = true;
}, 4000);
}
checkSaldo(amount: any, currencyCode: any): Promise<string> {
return new Promise((resolve) => {
this.restService.getByParam('recalculatedSaldo', { amount: amount, currency: currencyCode }).subscribe(response => {
resolve(response.payload);
});
})
}
แต่ฉันได้รับข้อผิดพลาดบนตัวแปร toolTip:
ชนิด 'Promise' ไม่สามารถกำหนดค่าให้กับชนิด 'string' ได้
มีข้อเสนอแนะอะไรไหม?
คำตอบ #1
คุณไม่สามารถกำหนด Promise ลงในฟิลด์ string ได้
แต่คุณต้องรอการเรียกกลับของ Promise และกำหนดค่าดังนี้:
this.checkSaldo(10, 'FRA').then((x) => {
this.toolTip = x;
...
});
ตัวอย่างสาธิต StackBlitz
