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
相关推荐
hxjhnct19 分钟前
React 为什么不采用(VUE)绑定数据?
javascript·vue.js·react.js
Knight_AL20 分钟前
Vue + Spring Boot 项目添加 /wvp 前缀的完整链路解析(从浏览器到静态资源)
前端·vue.js·spring boot
技术钱35 分钟前
vue3 + element plus实现表头拖拽数组进行汇总
前端·javascript·vue.js
chenhdowue35 分钟前
vue 如何实现 vxe-table 的按键操作回车键的上下移动修改为 Tab 键的左右切换
vue.js·vxe-table·vxe-ui
柒@宝儿姐37 分钟前
vue3中使用element-plus的el-scrollbar实现滚动触底加载更多
前端·javascript·vue.js
蜗牛攻城狮37 分钟前
深入理解 Vue.js 中的「运行时」与「编译时」:从模板到虚拟 DOM 的全过程
前端·javascript·vue.js
Knight_AL41 分钟前
Vue 项目部署在子目录下:hash vs history 的真实区别
前端·vue.js·哈希算法
zhaocarbon1 小时前
VUE 4向云台 8向云台UI
css·vue.js·ui
桜吹雪11 小时前
Vue 基础:状态管理入门
前端·vue.js
该换个名儿了15 小时前
Vue3中,我的Watch为什么总监听不到数据?
前端·javascript·vue.js