debounce-button.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <button :type="type" :class="['debounce-btn', disabled ? 'disabled' : '']" :disabled="disabled" @click="handleClick"
  3. :style="{backgroundColor:bgColor,color:color}" style="border-radius: 50px;" :loading="loading">
  4. <slot>{{ text }}</slot>
  5. </button>
  6. </template>
  7. <script>
  8. export default {
  9. props: {
  10. type: {
  11. type: String,
  12. default: null
  13. },
  14. color: {
  15. type: String,
  16. default: '#FFFFFF'
  17. },
  18. bgColor: {
  19. type: String,
  20. default: null
  21. },
  22. text: {
  23. type: String,
  24. default: '点击按钮'
  25. },
  26. debounceTime: {
  27. type: Number,
  28. default: 2000
  29. },
  30. loading: {
  31. type: Boolean,
  32. default: false
  33. }
  34. },
  35. data() {
  36. return {
  37. disabled: false,
  38. timer: null
  39. }
  40. },
  41. methods: {
  42. handleClick() {
  43. if (this.disabled) return
  44. this.disabled = true
  45. this.$emit('click')
  46. this.timer = setTimeout(() => {
  47. this.disabled = false
  48. }, this.debounceTime)
  49. }
  50. },
  51. beforeDestroy() {
  52. clearTimeout(this.timer)
  53. }
  54. }
  55. </script>
  56. <style scoped>
  57. .debounce-btn:active {
  58. opacity: 0.8;
  59. }
  60. .debounce-btn.disabled {
  61. cursor: not-allowed;
  62. opacity: 0.8;
  63. }
  64. </style>