Javascript 在lodash上使用冲洗';风门装饰器
使用TypeScript(JavaScript和Angular): 我希望lodash的throttle decorator在用户浏览页面时限制API调用,但在用户卸载(离开)站点之前仍会触发API调用 在typescript构造函数中,我有Javascript 在lodash上使用冲洗';风门装饰器,javascript,typescript,lodash,Javascript,Typescript,Lodash,使用TypeScript(JavaScript和Angular): 我希望lodash的throttle decorator在用户浏览页面时限制API调用,但在用户卸载(离开)站点之前仍会触发API调用 在typescript构造函数中,我有window.addEventListener('beforeunload',()=>this.onUnload()) 将onUnload()函数声明为 onUnload() { this.thisIsTheThrottledFunction.flu
window.addEventListener('beforeunload',()=>this.onUnload())代码>
将onUnload()
函数声明为
onUnload() {
this.thisIsTheThrottledFunction.flush;
}
但我得到的错误响应是“类型'()=>Promise'上不存在属性'flush'
我试图访问其.flush
方法的函数是另一个函数的声明限制版本。该函数已成功调节,因此我相信部分代码可以正常工作。访问.flush
方法的最佳方式是什么?您应该能够通过验证this
的值来调试它。在我看来,您只需要将对象的此
值传递给onUnload
函数(或者您可以传递它)。例如,您可以将其放入构造函数中:this.onUnload=this.onUnload.bind(this)
。这种语法有一个优点,在类中使用onUnload=()=>{…}
定义它。这两种方法都将方法附加到实例,而不是将其作为原型的一部分。或者,可以将绑定函数直接传递给事件侦听器:
window.addEventListener('beforeunload', this.onUnload.bind(this));
this.onUnload.bind(this))
是一个非常有用的注释,但没有解决导致错误响应的问题。通过将onUnload()
方法设置为(This.This是ottled函数)。flush()修复了此问题通过在上强制键入any
,此为锁定功能