Angular 使用window.requestAnimationFrame循环调用导致,异常ERROR TypeError: Cannot read properties of undefined (reading 'cube')
异常:

调用方法

当使用requestAnimationFrame调用animate()时出现异常
原因requestAnimationFrame调用时this是就不指当前对象了,this指向的就是requestAnimationFrame的内部
解决方法:
将animate()换成箭头方法
使用箭头方法,确保该方法没有自己的this,并将当前对象用作this。

或者在requestAnimationFrame处使用匿名箭头函数调用animate()进行循环


浙公网安备 33010602011771号