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
相关推荐
岁月宁静6 小时前
驾驭 AI 这匹野马:深入解析智能体 Harness 工程
vue.js·python
qq_2518364579 小时前
基于nodejs express +vue 天天商城系统设计与实现 (源码 文档)
前端·vue.js·express
前端毕业班11 小时前
uni-app 小程序样式隔离实践指南和原理分析
前端·javascript·vue.js
仿生狮子11 小时前
🎼 从文本到交互界面——GenUI 的中庸之道
前端·vue.js·markdown
用户8417948145613 小时前
vxe-gantt 甘特图在 Nuxt 中的集成与使用
vue.js
喵了几个咪13 小时前
AI重构软件开发范式:框架与脚手架为何仍是生产级开发的刚需?
vue.js·人工智能·react.js·重构·golang·ai编程
lpd_lt16 小时前
服务端类vue等页面AI测试方向
前端·vue.js·人工智能
橘子味的冰淇淋~16 小时前
优化前端性能之从“全局引入”改为“按需引入”
前端·javascript·vue.js
Vennn17 小时前
Android自动化:使用 Web 方式实现某音未读消息检查与采集
前端·javascript·vue.js
皮皮大人17 小时前
Vue 3 响应式内核完全解密:reactive & effect 与 Vue 2 Watcher 史诗对决
前端·vue.js