简体   繁体   English

如何使用 typescript 将 arguments 传递给 addEventListener 监听器 function?

[英]How to pass arguments to addEventListener listener function with typescript?

The situation is somewhat like:情况有点像:

const searchKeyPressHandler = (appDispatch: any, e: any) => {
    if (e.keyCode === 27) {
        appDispatch({ type: "closeSearch" })
    }
}

document.addEventListener("keyup", searchKeyPressHandler); // <-- error on searchKeyPressHandler
return () => document.removeEventListener("keyup", searchKeyPressHandler); // <-- error on searchKeyPressHandler

searchKeyPressHandler return error using typescript and I don't know how I can avoid it. searchKeyPressHandler使用 typescript 返回错误,我不知道如何避免它。

document.addEventListener("keyup", function (e) { searchKeyPressHandler(appDispatch, e) }); could be a solution for addEventListener可能是addEventListener的解决方案

but it is not useful for removeEventListener because the event will be never removed with return () => document.removeEventListener("keyup", function (e) { searchKeyPressHandler(appDispatch, e) });但它对removeEventListener没有用,因为事件永远不会被return () => document.removeEventListener("keyup", function (e) { searchKeyPressHandler(appDispatch, e) }); . .

错误

Just wrap the actual event handler in an anonymous function and that function can then call your function with its arguments.只需将实际事件处理程序包装在匿名 function 中,然后 function 就可以使用其 ZDBC11CABDABDA8 调用您的 ZC1C425268E68385D1AB5074C17A94F142E977776。 But don't forget that event handlers are automatically passed a reference to the event that triggered them, so capture that in the outer handler and pass it along with your other arguments:但是不要忘记事件处理程序会自动传递对触发它们的事件的引用,因此在外部处理程序中捕获它并将其与您的其他 arguments 一起传递:

document.addEventListener("keyup", function(event){ searchKeyPressHandler(event, x,y,z); });

Or, if you need to utilize the function that you are setting up as the "wrapper" more than once (in the case of having to remove the listener later), you'd just declare the function with a name and pass that name into .addEventListner() as follows:或者,如果您需要多次使用您设置为“包装器”的 function(在以后必须删除侦听器的情况下),您只需使用名称声明 function 并将该名称传递给.addEventListner()如下:

function handler(event){ 
  searchKeyPressHandler(event, x,y,z); 
}

document.addEventListener("keyup", handler);

// And later...
document.removeEventListener("keyup", handler);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将arguments传给addEventListener监听function? - How to pass arguments to addEventListener listener function? 如何将参数传递给addEventListener中传递的侦听器函数? - How to pass an argument to the listener function passed in addEventListener? 如何在.addEventListener中将其他变量传递给侦听器 - How to pass additional variable to a listener in .addEventListener JavaScript:如何将参数传递给最终将被删除的侦听器函数 - JavaScript: How to pass arguments to a listener function that will eventually be removed 如何在不创建新函数的情况下将参数传递给React + Typescript中的回调? - How to pass arguments to callback in React + Typescript without new function creation? 如何将 arguments 传递给 typescript 中的内外 function? - How to pass arguments to both inner and outer function in typescript? 将带有参数的函数传递给`addEventListener` - Passing a function with arguments to `addEventListener` JavaScript:如何将对象作为参数传递给addEventListener函数? - JavaScript : How to pass object as argument to addEventListener function? 如何将参数传递给 addEventListener 中的外部函数 - How to pass an argument to the external function in addEventListener 如何将 querySelector 元素选择的元素传递给 addEventListener 中的函数? - How to pass selected by querySelector element into a function in addEventListener?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM