debounce-button.vue 1.3 KB

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