0

我正在为侦听“window:keydown”的组件编写测试。我尝试了几种方法,但没有一种方法有效:

element.dispatchEvent(new testWindow.Event("window:keydown"))
window.dispatchEvent(new testWindow.Event("window:keydown"))
window.dispatchEvent(new Event("keydown"))
window.dispatchEvent(new Event("window:keydown"))

相反,正确的语法是什么?

4

1 回答 1

0

我是这样做的:

const keyCode = 37; // Left, but can be done wherever
const keyboardOptions: any = { bubbles: true, cancelable: true, keyCode };
document.dispatchEvent(new KeyboardEvent("keydown", keyboardOptions));

有关更详细的示例,请参阅https://github.com/mattdsteele/joy-con-element/blob/master/src/components/joy-con/joy-con.tsx#L65 。

于 2018-09-01T00:31:41.500 回答