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
相关推荐
lemon_yyds6 小时前
《vue 2 升级vue3 父组件 子组件 传值: value 和 v-model
vue.js
simple_lau7 小时前
Cursor配置MasterGo MCP:一键读取设计稿生成高还原度前端代码
前端·javascript·vue.js
睡不着先生7 小时前
如何设计一个真正可扩展的表单生成器?
前端·javascript·vue.js
wuhen_n10 小时前
Pinia状态管理原理:从响应式核心到源码实现
前端·javascript·vue.js
wuhen_n10 小时前
KeepAlive:组件缓存实现深度解析
前端·javascript·vue.js
wuhen_n10 小时前
Vue Router与响应式系统的集成
前端·javascript·vue.js
Ruihong11 小时前
《VuReact:下一代 Vue 3 -> React 智能编译工具,支持 SFC 与增量迁移》
vue.js
lemon_yyds11 小时前
vue 2 升级vue3 : ref 和 v-model 命名为同名
前端·vue.js
前端Hardy14 小时前
告别 !important:现代 CSS 层叠控制指南,90% 的样式冲突其实不用它也能解
前端·vue.js·面试
前端Hardy14 小时前
Vue 3 性能优化的 5 个隐藏技巧,第 4 个连老手都未必知道
前端·vue.js·面试