QmButton.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <template>
  2. <!-- FIXED: 从 vue 3.1.5 升级到 3.2.1 时这里出错了,slot也要有key -->
  3. <a-button v-bind="newAttrs" :loading="inInterval" @click="insideClick">
  4. <template v-for="(_, slot) of $slots" v-slot:[slot]="scope">
  5. <slot :name="asAny(slot)" v-bind="asAny(scope)" :key="slot" />
  6. </template>
  7. <!-- <slot name="default" /> -->
  8. </a-button>
  9. </template>
  10. <script lang="ts">
  11. import { reactive, ref } from "vue";
  12. // 默认loading一秒,以免重复点击
  13. export default {
  14. name: "QmButton",
  15. inheritAttrs: false,
  16. props: {
  17. clickTimeout: { type: Number, required: false, default: 1000 },
  18. },
  19. // @ts-ignore
  20. setup(props, { attrs }) {
  21. let newAttrs = reactive({});
  22. Object.assign(newAttrs, attrs);
  23. let parentOnClick = attrs.onClick;
  24. // @ts-ignore
  25. delete newAttrs["onClick"];
  26. let inInterval = ref(false);
  27. const insideClick = (e: MouseEvent) => {
  28. inInterval.value = true;
  29. setTimeout(() => (inInterval.value = false), props.clickTimeout);
  30. parentOnClick(e);
  31. };
  32. // newAttrs.onClick = insideClick;
  33. function asAny(input: any): any {
  34. return input as any;
  35. }
  36. return { newAttrs, inInterval, insideClick, asAny };
  37. },
  38. };
  39. </script>