QmButton.vue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <template>
  2. <!-- TODO: 应该用注释的这段代码的,但是从 vue 3.1.5 升级到 3.2.1 时这里出错了 -->
  3. <!-- <template v-for="(_, slot) of $slots" v-slot:[slot]="scope">
  4. <slot :name="slot" v-bind="scope" />
  5. </template> -->
  6. <a-button v-bind="newAttrs" :loading="inInterval.value" @click="insideClick">
  7. <slot name="default" />
  8. </a-button>
  9. </template>
  10. <script lang="ts">
  11. import { reactive, ref } from "@vue/reactivity";
  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. return { newAttrs, inInterval, insideClick };
  34. },
  35. };
  36. </script>