鍍金池/ 問答/數(shù)據(jù)庫  HTML/ VUE是否有必要利用事件委托

VUE是否有必要利用事件委托

VUE是否有必要利用事件委托,還是VUE底層源碼已經(jīng)進行過處理!
例如
<ul>
<li v-for="....."></li>
</ul>
這種的話在UL標簽上添加事件好,還是在LI上添加事件?還是說VUE在底層已經(jīng)進行過處理,無所謂在哪里添加事件?

回答
編輯回答
念初

官方不推薦使用事件委托。

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.

2017年12月20日 00:57