typescript - Angular D3 - คุณสมบัติ 'getBoundingClientRect' ไม่มีอยู่ในประเภท 'Window'
ฉันมี stackblitz อยู่ที่นี่ - https://stackblitz.com/edit/ng-tootltip-ocdngb?file=src/app/bar-chart.ts
ฉันมีแผนภูมิ D3 ในแอป Angular
แถบจะแสดงเคล็ดลับเครื่องมือเมื่อเอาเคอร์เซอร์ไปวางบนมัน
บนหน้าจอขนาดเล็ก tooltip จะอยู่ตรงกลางของหน้าต่าง
ในการทำเช่นนี้ฉันต้องการความกว้างของเครื่องมือแนะนำซึ่งฉันได้รับด้วย
const toolTipWidth = tooltip.node().getBoundingClientRect().width;
นี่ทำงานได้ดีที่นี่ แต่แอปจริงของฉันเป็นแอป Angular CLI
แอปยังคงทำงานอยู่ แต่ฉันได้รับข้อผิดพลาด
error TS2339: Property 'getBoundingClientRect' does not exist on type 'BaseType'.
Property 'getBoundingClientRect' does not exist on type 'Window'.
มันคือข้อผิดพลาดหรือไม่และฉันสามารถหยุดมันได้หรือไม่
คำตอบ #1
คุณสามารถง่าย ๆ แค่แปลง tooltip.node() เป็น any เพื่อหลีกเลี่ยงปัญหานี้:
const toolTipWidth = (tooltip.node() as any).getBoundingClientRect().width;
ชนิดที่ถูกต้องน่าจะเป็น HTMLElement ซึ่งนั่นก็น่าจะใช้งานได้เช่นกัน:
const toolTipWidth = (tooltip.node() as HTMLElement).getBoundingClientRect().width;
