当前位置: 首页 > article >正文

vue3 ts button 组件封装

vue文件

<template>
  <button
    class="button"
    :class="[
      `button-${type}`,
      {
        'is-plain': plain,
        'is-round': round,
        'is-circle': circle,
        'is-disabled': disabled,
      },
    ]"
    :disabled="disabled"
    @click="clickHandle"
  >
    <i v-if="icon" :class="`icon-${icon}`"></i>
    <span v-if="$slots.default">
      <slot></slot>
    </span>
  </button>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { buttonProps } from "./button";
export default defineComponent({
  name: "Button",
  props: buttonProps,
  emits: ["click"],
  setup(_, { emit }) {
    function clickHandle(e: Event) {
      emit("click", e);
    }
    return {
      clickHandle,
    };
  },
});
</script>
<!-- <script setup lang='ts'>
defineOptions({
  name: 'Button',
})

import { buttonProps, buttonEmits } from "./button";
defineProps(buttonProps)
const emit = defineEmits(buttonEmits)
const clickHandle = (e: Event) => {
  emit('click', e)
}
</script> -->
<style scoped></style>

ts文件

// 该文件用于定义 button 的 props 属性
// 将 props 定义为 button 的类型
// ExtractPropTypes 是 vue3 所提供的一个工具类型
// 用于从 vue 组件的 props 对象中提取 ts 类型

import type { ExtractPropTypes } from "vue";
// 定义 props
export const buttonProps = {
  type: {
    type: String,
    default: "default",
  },
  plain: {
    type: Boolean,
    default: false,
  },
  round: {
    type: Boolean,
    default: false,
  },
  circle: {
    type: Boolean,
    default: false,
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  icon: {
    type: String,
    default: "",
  },
};

export const buttonEmits = {
  click: (e: MouseEvent) => e instanceof MouseEvent,
}

export type ButtonProps = ExtractPropTypes<typeof buttonProps>;
export type ButtonEmits = typeof buttonEmits

http://www.kler.cn/a/416678.html

相关文章:

  • Docker 清理镜像策略详解
  • Vue3组件通信的8种方式,完整源码带注释
  • 图解:XSS攻击原理与安全过滤
  • ip租期到了
  • Qt关于padding设置不起作用的的解决办法
  • 【C++】泛型算法(三):定制操作
  • Android 图形系统之三:SurfaceControl
  • aws rds-mysql不支持性能详情监控
  • Jackson库中@JsonAlias和@JsonProperty的使用和区别
  • 挑战用React封装100个组件【002】
  • 2025年人工智能,自动化与机械工程国际学术会议(AIAME2025)
  • 颜色分类
  • CPU性能优化-CPU特性
  • 求100之内的素数-多语言
  • 分布式锁的实现原理
  • HarmonyOS-中级(一)
  • 嵌入式QT学习第4天:Qt 信号与槽
  • 无人机数据处理系统:原理与核心系统
  • 12 设计模式之工厂方法模式
  • 华为OD机试真题-数组组成的最小数-2024年OD统一考试(E卷)