Button 按钮组件
组件说明
基于 Ant Design Vue Button 封装的增强型按钮组件,支持图标、加载状态、颜色主题等功能。
组件路径
src/components/Button/
├── index.ts
├── src/
│ ├── BasicButton.vue
│ ├── JUploadButton.vue
│ ├── PopConfirmButton.vue
│ └── props.ts导入方式
import { Button, JUploadButton, PopConfirmButton } from '@/components/Button';Button 基础按钮
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| type | 按钮类型 | 'primary' | 'default' | 'dashed' | 'link' | 'text' | 'default' | 否 |
| color | 按钮颜色主题 | 'error' | 'warning' | 'success' | '' | - | 否 |
| loading | 加载状态 | Boolean | false | 否 |
| disabled | 禁用状态 | Boolean | false | 否 |
| preIcon | 前置图标 | String | - | 否 |
| postIcon | 后置图标 | String | - | 否 |
| iconSize | 图标大小 | Number | 15 | 否 |
| isUpload | 是否上传按钮 | Boolean | false | 否 |
| onClick | 点击事件 | (...args) => any | - | 否 |
| htmlType | 按钮 HTML 类型 | 'button' | 'submit' | 'reset' | 'button' | 否 |
| danger | 危险按钮 | Boolean | false | 否 |
| ghost | 幽灵属性 | Boolean | false | 否 |
| block | 块级按钮 | Boolean | false | 否 |
| shape | 按钮形状 | 'default' | 'circle' | 'round' | 'default' | 否 |
| size | 按钮大小 | 'large' | 'middle' | 'small' | 'middle' | 否 |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| click | 点击按钮 | (event: MouseEvent) => void |
Slots
| 插槽名 | 说明 |
|---|---|
| default | 按钮内容 |
| icon | 图标插槽(前置图标) |
使用示例
<template>
<div class="button-demo">
<!-- 基础用法 -->
<Button type="primary">主要按钮</Button>
<Button type="default">默认按钮</Button>
<Button type="dashed">虚线按钮</Button>
<Button type="link">链接按钮</Button>
<Button type="text">文本按钮</Button>
<!-- 颜色主题 -->
<Button color="success">成功按钮</Button>
<Button color="warning">警告按钮</Button>
<Button color="error">错误按钮</Button>
<!-- 带图标 -->
<Button preIcon="ant-design:search-outlined">搜索</Button>
<Button postIcon="ant-design:download-outlined">下载</Button>
<Button preIcon="ant-design:plus-outlined">新建</Button>
<!-- 加载状态 -->
<Button :loading="loading" @click="handleLoad">加载中</Button>
<!-- 禁用状态 -->
<Button disabled>禁用按钮</Button>
<!-- 不同尺寸 -->
<Button size="large">大号按钮</Button>
<Button size="middle">中号按钮</Button>
<Button size="small">小号按钮</Button>
<!-- 块级按钮 -->
<Button block>块级按钮</Button>
<!-- 危险按钮 -->
<Button danger>危险操作</Button>
<!-- 圆形按钮 -->
<Button shape="circle" preIcon="ant-design:search-outlined" />
<!-- 圆角按钮 -->
<Button shape="round">圆角按钮</Button>
<!-- 幽灵按钮 -->
<Button ghost>幽灵按钮</Button>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Button } from '@/components/Button';
const loading = ref(false);
const handleLoad = () => {
loading.value = true;
setTimeout(() => {
loading.value = false;
}, 2000);
};
</script>
<style scoped>
.button-demo {
display: flex;
flex-direction: column;
gap: 16px;
}
.button-demo > div {
display: flex;
gap: 8px;
align-items: center;
}
</style>JUploadButton 上传按钮
Props
继承 Button 的所有 Props,额外支持:
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| action | 上传地址 | String | - | 是 |
| headers | 请求头 | Object | - | 否 |
| name | 文件字段名 | String | 'file' | 否 |
| data | 附带数据 | Object | - | 否 |
| maxSize | 最大文件大小(MB) | Number | 10 | 否 |
| accept | 接受的文件类型 | String | - | 否 |
| multiple | 多选文件 | Boolean | false | 否 |
| beforeUpload | 上传前处理 | (file) => boolean | Promise | - | 否 |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| change | 文件状态改变 | (fileList) => void |
| success | 上传成功 | (response, file) => void |
| error | 上传失败 | (error) => void |
使用示例
<template>
<div class="upload-demo">
<!-- 基础上传 -->
<JUploadButton
action="/api/upload"
@success="handleSuccess"
>
点击上传
</JUploadButton>
<!-- 图片上传 -->
<JUploadButton
action="/api/upload"
accept="image/*"
:maxSize="5"
@success="handleSuccess"
>
上传图片
</JUploadButton>
<!-- 多文件上传 -->
<JUploadButton
action="/api/upload"
:multiple="true"
@change="handleChange"
>
上传多个文件
</JUploadButton>
</div>
</template>
<script setup lang="ts">
import { JUploadButton } from '@/components/Button';
const handleSuccess = (response: any, file: any) => {
console.log('上传成功:', response, file);
};
const handleChange = (fileList: any[]) => {
console.log('文件列表变化:', fileList);
};
</script>PopConfirmButton 气泡确认按钮
Props
继承 Button 的所有 Props,额外支持:
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| title | 确认标题 | String | - | 是 |
| description | 确认描述 | String | - | 否 |
| okText | 确认按钮文本 | String | '确定' | 否 |
| cancelText | 取消按钮文本 | String | '取消' | 否 |
| okType | 确认按钮类型 | String | 'primary' | 否 |
| okButtonProps | 确认按钮配置 | Object | - | 否 |
| cancelButtonProps | 取消按钮配置 | Object | - | 否 |
| icon | 自定义图标 | String | 'ant-design:question-circle-outlined' | 否 |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| confirm | 点击确认 | (e: MouseEvent) => void |
| cancel | 点击取消 | (e: MouseEvent) => void |
| visibleChange | 显示状态变化 | (visible: boolean) => void |
使用示例
<template>
<div class="popconfirm-demo">
<!-- 基础用法 -->
<PopConfirmButton
title="确定要删除吗?"
@confirm="handleDelete"
>
删除
</PopConfirmButton>
<!-- 带描述 -->
<PopConfirmButton
title="确定要删除吗?"
description="删除后无法恢复"
@confirm="handleDelete"
>
删除
</PopConfirmButton>
<!-- 自定义按钮 -->
<PopConfirmButton
title="确定要提交吗?"
okText="提交"
cancelText="再想想"
okType="primary"
@confirm="handleSubmit"
>
提交
</PopConfirmButton>
<!-- 危险操作 -->
<PopConfirmButton
title="确定要删除吗?"
color="error"
@confirm="handleDelete"
>
删除
</PopConfirmButton>
<!-- 带图标 -->
<PopConfirmButton
title="确定要退出吗?"
preIcon="ant-design:logout-outlined"
@confirm="handleLogout"
>
退出登录
</PopConfirmButton>
</div>
</template>
<script setup lang="ts">
import { PopConfirmButton } from '@/components/Button';
const handleDelete = (e: MouseEvent) => {
console.log('确认删除', e);
};
const handleSubmit = (e: MouseEvent) => {
console.log('确认提交', e);
};
const handleLogout = (e: MouseEvent) => {
console.log('确认退出', e);
};
</script>组合使用
<template>
<div class="button-group">
<!-- 操作按钮组 -->
<Space>
<Button type="primary" preIcon="ant-design:plus-outlined">
新建
</Button>
<Button preIcon="ant-design:edit-outlined">
编辑
</Button>
<PopConfirmButton
title="确定要删除吗?"
color="error"
@confirm="handleDelete"
>
删除
</PopConfirmButton>
</Space>
<!-- 表格操作列 -->
<Table :columns="columns" :data="data">
<template #action="{ record }">
<Space>
<Button type="link" @click="handleEdit(record)">
编辑
</Button>
<PopConfirmButton
title="确定要删除吗?"
@confirm="handleDelete(record)"
>
<Button type="link" color="error">删除</Button>
</PopConfirmButton>
</Space>
</template>
</Table>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Button, PopConfirmButton } from '@/components/Button';
import { Space, Table } from 'ant-design-vue';
const columns = ref([
{ title: '姓名', dataIndex: 'name' },
{ title: '年龄', dataIndex: 'age' },
{ title: '操作', dataIndex: 'action', slots: { customRender: 'action' } },
]);
const data = ref([
{ name: '张三', age: 25 },
{ name: '李四', age: 30 },
]);
const handleEdit = (record: any) => {
console.log('编辑:', record);
};
const handleDelete = (record: any) => {
console.log('删除:', record);
};
</script>样式定制
按钮组件支持通过以下方式定制样式:
1. 颜色主题
<template>
<!-- 使用内置颜色 -->
<Button color="success">成功</Button>
<Button color="warning">警告</Button>
<Button color="error">错误</Button>
<!-- 自定义样式 -->
<Button class="custom-button">自定义</Button>
</template>
<style scoped>
.custom-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border: none;
color: white;
}
</style>2. 图标大小
<template>
<!-- 使用 iconSize 属性 -->
<Button preIcon="ant-design:search-outlined" :iconSize="20">
搜索
</Button>
<!-- 默认 15px -->
<Button preIcon="ant-design:plus-outlined">
添加
</Button>
</template>注意事项
- 图标使用: 图标使用
@iconify的图标名称,格式为ant-design:icon-name - 按钮类型:
type属性优先级高于color属性 - 加载状态: 设置
loading后按钮会被禁用,无法点击 - 上传按钮:
JUploadButton需要配置action上传地址 - 确认按钮:
PopConfirmButton的title是必填项
类型定义
export interface ButtonProps {
type?: 'primary' | 'default' | 'dashed' | 'link' | 'text';
color?: 'error' | 'warning' | 'success' | '';
loading?: boolean;
disabled?: boolean;
preIcon?: string;
postIcon?: string;
iconSize?: number;
isUpload?: boolean;
onClick?: (...args: any[]) => any;
}
export type ButtonPropsType = Partial<ButtonProps>;组件文档版本: 1.0.0 最后更新: 2026-01-10