vue 基于element-plus el-button封装按钮组件

封装组件的原则是:组件只是数据流通的一个管道,不要糅合太多的逻辑在里面,是一个纯组件,还要根据自己项目的业务场景做具体的处理。

javascript 复制代码
// MyButton.vue
// 基于element-plus中el-button来封装按钮
<template>
    <el-button @click="handleClick">
        <div class="btn-text-style"> // 文本样式
            <slot></slot> // 预留按钮文本插槽
        </div>
    </el-button>
</template>

<script setup>
const emits = defineEmits(['click'])
// 出发click事件
const handleClick = () => {
    emits('click')
}

</script>

<style>
.btn-text-style {
    font-size: 14px;
    font-weight: bold;
    font-family: 'Courier New', Courier, monospace;
}
</style>
javascript 复制代码
// 在具体组件中的使用
// 根据透传Attributes 即属性的继承
// 透传 attribute 指的是传递给一个组件,却没有被该组件声明为 props 或 emits 的 attribute
// 或者 v-on 事件监听器。最常见的例子就是 class、style 和 id。
// 我们在父组件中添加的各种属性都会被子组件继承下来,所以有了 type, size, icon等这些属性
<template #footer>
    <span class="dialog-footer">
        <MyButton @click="cancel" type="info" size="mini" disabeld icon="Edit">取消</MyButton>
        <MyButton type="primary" @click="submitForm">确定</MyButton>
    </span>
</template>
相关推荐
LaughingZhu5 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫5 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux5 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水6 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger6 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)7 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态7 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态7 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart7 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe57 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架