待劫持的组件

  1. <template>
  2. <button @click="handleClick">
  3. <slot></slot>
  4. </button>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'XButton',
  9. methods: {
  10. handleClick (event) {
  11. this.$emit('click', event)
  12. }
  13. }
  14. }
  15. </script>
  16. <style scoped>
  17. </style>

劫持的逻辑

  1. import xButton from '../components/button'
  2. // 防抖函数
  3. function debounce (func, delay, context, args) {
  4. clearTimeout(func.timer)
  5. func.timer = setTimeout(function () {
  6. func.call(context, ...args)
  7. }, delay)
  8. }
  9. function proxy (func) {
  10. return function () {
  11. console.log('debounce')
  12. debounce(func, 300, this, arguments)
  13. }
  14. }
  15. // 导出新组件
  16. export default {
  17. functional: true,
  18. render (h, context) {
  19. context.listeners.click = proxy(context.listeners.click)
  20. return h(xButton, context.data, context.children)
  21. }
  22. }