简体   繁体   English

svelte 组件中的事件处理

[英]event handling in svelte component

let's say we create a custom element called MyAnchor, which will have click event handler, and intercept click, and for some, we will have custom dialog, then my question is, does svelte assigns event handler over every instance of that component in a page, or one delegated event handler to all instances, passing ref?假设我们创建了一个名为 MyAnchor 的自定义元素,它将具有点击事件处理程序,并拦截点击,对于某些人,我们将拥有自定义对话框,那么我的问题是,svelte 是否为页面中该组件的每个实例分配事件处理程序或一个委托给所有实例的事件处理程序,传递 ref? question is generic for any component.问题对于任何组件都是通用的。

Svelte assigns your event handler to each instance. Svelte 将您的事件处理程序分配给每个实例。

You can see this by looking at the output of the compiler.您可以通过查看编译器的输出来看到这一点。 (See "JS output" tab in https://svelte.dev/repl ) (参见https://svelte.dev/repl 中的“JS 输出”选项卡)

For example:例如:

<MyAnchor on:click={handleClick}/>
<MyAnchor on:click={handleClick}/>

Will result in:会导致:

const myanchor0 = new MyAnchor();
myanchor0.$on("click", handleClick);

const myanchor1 = new MyAnchor();
myanchor1.$on("click", handleClick);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM