关于前端:vue中是否有必要使用事件委托

42次阅读

共计 815 个字符,预计需要花费 3 分钟才能阅读完成。

vue 中是否有必要应用事件委托

原问答链接

Is event delegation necessary

论断

Well, delegation has two main advantages: one is practical – it saves you from having to add (and remove!!) those listeners individually. But Vue already does that for you.

The other one is performance / memory. But since every click listener in a v-vor loop would use the same callback, this is minimal unless you have hundreds or thousands of rows.

And finally, you can use delegation pretty easily by adding an @click listener to the <ul> element instead of the children. But then you have to resort to checks on the click target to evaluate which item in your data it might represent. So I would only use that if you truly find any performance problems without delegation.

翻译

事件委托有两个次要长处:其中一个是实用的,它使你免于独自的给每个元素增加和移除 listener,然而这件事 vue 曾经帮你做了

另一个长处是性能,然而因为循环中每个 listener 会应用同样的回调,除非有成千盈百行,否则影响是极小的

最初,通过在 <ul> 上增加 click 事件而不是在其子元素上增加你能够很容易的实现事件委托,然而你必须用额定的代码查看 click target。所以我只有在不应用委托的确造成了性能问题时应用

正文完
 0