render
和 vm.$scopedSlots.default()
vm.$scopedSlots.default()
获取插槽vnode下面见代码node
<script> import _ from 'lodash' const throttle = function (fn, wait = 50, ctx) { let timer let lastCall = 0 return function (...params) { const now = new Date().getTime() if (now - lastCall < wait) return lastCall = now fn.apply(ctx, params) } } export default { props: { time: Number, eventNameList: Array }, abstract: true, data() { return { originMap: {}, throttledMap:{} } }, render (h) { let vnode = this.$scopedSlots.default()[0]; // /// 全部的 $slots 如今都会做为函数暴露在 $scopedSlots 中。若是你在使用渲染函数,不论当前插槽是否带有做用域,咱们都推荐始终经过 $scopedSlots 访问它们。这不单单使得在将来添加做用域变得简单,也能够让你最终轻松迁移到全部插槽都是函数的 Vue 3。 // let vnode = this.$slots.default[0]; this.eventNameList.forEach(element => { let targetEvent = vnode.data.on[element] // 获取插槽节点的事件 if (targetEvent&& this.originMap[element]&&this.throttledMap[element]) { vnode.data.on[element] = this.throttledMap[element] } else if(targetEvent) { this.originMap[element] = targetEvent; this.throttledMap[element] = _.throttle(targetEvent,this.time); vnode.data.on[element] = _.throttle(targetEvent,this.time); } }); return vnode; } } </script>
import throttle from './throttle' <throttle :time='2000' :eventNameList="eventNameList"> <button @click='clickSolt'>点击</button> </throttle>