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。所以我只有在不使用委托确实造成了性能问题时使用


suri
39 声望1 粉丝