vue3 ts button 组件封装

vue文件

csharp 复制代码
<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文件

csharp 复制代码
// 该文件用于定义 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
相关推荐
代码熊崽的编程森林10 小时前
vue + onlyoffice 自定义插件的实现(OnlyOffice 插件:AI 智能编辑)。
前端·javascript·vue.js
Lucky_Turtle10 小时前
【Vue】element plus Slider小数组件设置顺滑程度
前端·javascript·vue.js
上单带刀不带妹11 小时前
Vue3 中 getCurrentInstance() 与 proxy 详解
前端·javascript·vue.js
许彰午12 小时前
32 个 Vue 组件的设计取舍
前端·javascript·vue.js
前端繁华如梦13 小时前
不写模型文件,用代码「捏」出 3D 世界:Vue3 + Three.js 程序化资产生成实战
前端·vue.js
劉三岁14 小时前
Git 给 main 分支打 Tag(版本标记)完整教程
vue.js·github
zifengningyu14 小时前
【无标题】
前端·vue.js
李铁蛋zs16 小时前
AI 前端开发 Prompt 模板库
前端·vue.js·prompt
Ruihong16 小时前
VuReact v1.8.4 发布:Vue 迁移 React 编译器迎来稳定性大修,这些坑终于被填平了
前端·vue.js·react.js
国思RDIF框架16 小时前
国思 RDIF 低代码快速开发框架 v6.3 版本重磅发布!性能与体验双飞跃
前端·vue.js·后端