Button 按钮
常用的操作按钮。
基础用法
使用 type
、plain
、round
和 circle
来定义按钮的样式。
<template>
<el-row class="mb-4">
<el-button>Default</el-button>
<el-button type="primary">Primary</el-button>
<el-button type="success">Success</el-button>
<el-button type="info">Info</el-button>
<el-button type="warning">Warning</el-button>
<el-button type="danger">Danger</el-button>
</el-row>
<el-row class="mb-4">
<el-button plain>Plain</el-button>
<el-button type="primary" plain>Primary</el-button>
<el-button type="success" plain>Success</el-button>
<el-button type="info" plain>Info</el-button>
<el-button type="warning" plain>Warning</el-button>
<el-button type="danger" plain>Danger</el-button>
</el-row>
<el-row class="mb-4">
<el-button round>Round</el-button>
<el-button type="primary" round>Primary</el-button>
<el-button type="success" round>Success</el-button>
<el-button type="info" round>Info</el-button>
<el-button type="warning" round>Warning</el-button>
<el-button type="danger" round>Danger</el-button>
</el-row>
<el-row>
<el-button :icon="Search" circle />
<el-button type="primary" :icon="Edit" circle />
<el-button type="success" :icon="Check" circle />
<el-button type="info" :icon="Message" circle />
<el-button type="warning" :icon="Star" circle />
<el-button type="danger" :icon="Delete" circle />
</el-row>
</template>
<script lang="ts" setup>
import {
Check,
Delete,
Edit,
Message,
Search,
Star,
} from '@element-plus/icons-vue'
</script>
禁用状态
你可以使用 disabled
属性来定义按钮是否被禁用。
使用 disabled
属性来控制按钮是否为禁用状态。 该属性接受一个 Boolean
类型的值。
<template>
<el-row class="mb-4">
<el-button disabled>Default</el-button>
<el-button type="primary" disabled>Primary</el-button>
<el-button type="success" disabled>Success</el-button>
<el-button type="info" disabled>Info</el-button>
<el-button type="warning" disabled>Warning</el-button>
<el-button type="danger" disabled>Danger</el-button>
</el-row>
<el-row>
<el-button plain disabled>Plain</el-button>
<el-button type="primary" plain disabled>Primary</el-button>
<el-button type="success" plain disabled>Success</el-button>
<el-button type="info" plain disabled>Info</el-button>
<el-button type="warning" plain disabled>Warning</el-button>
<el-button type="danger" plain disabled>Danger</el-button>
</el-row>
</template>
链接按钮
WARNING
type="text"
已被 废弃,将于版本 3.0.0 时 移除,请考虑切换至新的 API。
新的 API link
于 2.2.1 版本添加,同时可以使用 type
API 设置链接按钮的主题样式。
<template>
<p>Basic link button</p>
<div class="flex justify-space-between mb-4 flex-wrap gap-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
link
>{{ button.text }}</el-button
>
</div>
<p>Disabled link button</p>
<div class="flex justify-space-between flex-wrap gap-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
link
disabled
>{{ button.text }}</el-button
>
</div>
</template>
<script setup lang="ts">
const buttons = [
{ type: '', text: 'plain' },
{ type: 'primary', text: 'primary' },
{ type: 'success', text: 'success' },
{ type: 'info', text: 'info' },
{ type: 'warning', text: 'warning' },
{ type: 'danger', text: 'danger' },
] as const
</script>
文字按钮
TIP
文字按钮在现在有了全新的设计样式。 2.2.0 如果您想要使用老版样式的按钮,可以考虑使用 Link 组件。
API也已更新,由于 type
属性会同时控制按钮的样式, 因此我们通过一个新的 API text: boolean
来控制文字按钮。
没有边框和背景色的按钮。
<template>
<p>Basic text button</p>
<div class="flex justify-space-between mb-4 flex-wrap gap-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
text
>{{ button.text }}</el-button
>
</div>
<p>Background color always on</p>
<div class="flex justify-space-between mb-4 flex-wrap gap-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
text
bg
>{{ button.text }}</el-button
>
</div>
<p>Disabled text button</p>
<div class="flex justify-space-between flex-wrap gap-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
text
disabled
>{{ button.text }}</el-button
>
</div>
</template>
<script setup lang="ts">
const buttons = [
{ type: '', text: 'plain' },
{ type: 'primary', text: 'primary' },
{ type: 'success', text: 'success' },
{ type: 'info', text: 'info' },
{ type: 'warning', text: 'warning' },
{ type: 'danger', text: 'danger' },
] as const
</script>
图标按钮
使用图标为按钮添加更多的含义。 你也可以单独使用图标不添加文字来节省显示区域占用。
使用 icon
属性来为按钮添加图标。 您可以在我们的 Icon 组件中找到所需图标。 通过向右方添加<i>
标签来添加图标, 你也可以使用自定义图标。
<template>
<div class="flex">
<el-button type="primary" :icon="Edit" />
<el-button type="primary" :icon="Share" />
<el-button type="primary" :icon="Delete" />
<el-button type="primary" :icon="Search">Search</el-button>
<el-button type="primary">
Upload<el-icon class="el-icon--right"><Upload /></el-icon>
</el-button>
</div>
</template>
<script setup lang="ts">
import { Delete, Edit, Search, Share, Upload } from '@element-plus/icons-vue'
</script>
按钮组
以按钮组的方式出现,常用于多项类似操作。
使用 <el-button-group>
对多个按钮分组。
<template>
<el-button-group>
<el-button type="primary" :icon="ArrowLeft">Previous Page</el-button>
<el-button type="primary">
Next Page<el-icon class="el-icon--right"><ArrowRight /></el-icon>
</el-button>
</el-button-group>
<el-button-group class="ml-4">
<el-button type="primary" :icon="Edit" />
<el-button type="primary" :icon="Share" />
<el-button type="primary" :icon="Delete" />
</el-button-group>
</template>
<script setup lang="ts">
import {
ArrowLeft,
ArrowRight,
Delete,
Edit,
Share,
} from '@element-plus/icons-vue'
</script>
加载状态按钮
点击按钮来加载数据,并向用户反馈加载状态。
通过设置 loading
属性为 true
来显示加载中状态。
TIP
您可以使用 loading
插槽或 loadingIcon
属性自定义您的loading图标
ps: loading
插槽优先级高于loadingIcon
属性
<template>
<el-button type="primary" loading>Loading</el-button>
<el-button type="primary" :loading-icon="Eleme" loading>Loading</el-button>
<el-button type="primary" loading>
<template #loading>
<div class="custom-loading">
<svg class="circular" viewBox="-10, -10, 50, 50">
<path
class="path"
d="
M 30 15
L 28 17
M 25.61 25.61
A 15 15, 0, 0, 1, 15 30
A 15 15, 0, 1, 1, 27.99 7.5
L 15 15
"
style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"
/>
</svg>
</div>
</template>
Loading
</el-button>
</template>
<script lang="ts" setup>
import { Eleme } from '@element-plus/icons-vue'
</script>
<style scoped>
.el-button .custom-loading .circular {
margin-right: 6px;
width: 18px;
height: 18px;
animation: loading-rotate 2s linear infinite;
}
.el-button .custom-loading .circular .path {
animation: loading-dash 1.5s ease-in-out infinite;
stroke-dasharray: 90, 150;
stroke-dashoffset: 0;
stroke-width: 2;
stroke: var(--el-button-text-color);
stroke-linecap: round;
}
</style>
调整尺寸
除了默认的大小,按钮组件还提供了几种额外的尺寸可供选择,以便适配不同的场景。
使用 size
属性额外配置尺寸,可使用 large
和small
两种值。
<template>
<el-row>
<el-button size="large">Large</el-button>
<el-button>Default</el-button>
<el-button size="small">Small</el-button>
<el-button size="large" :icon="Search">Search</el-button>
<el-button :icon="Search">Search</el-button>
<el-button size="small" :icon="Search">Search</el-button>
</el-row>
<el-row class="my-4">
<el-button size="large" round>Large</el-button>
<el-button round>Default</el-button>
<el-button size="small" round>Small</el-button>
<el-button size="large" :icon="Search" round>Search</el-button>
<el-button :icon="Search" round>Search</el-button>
<el-button size="small" :icon="Search" round>Search</el-button>
</el-row>
<el-row>
<el-button :icon="Search" size="large" circle />
<el-button :icon="Search" circle />
<el-button :icon="Search" size="small" circle />
</el-row>
</template>
<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
</script>
自定义颜色 beta
您可以自定义按钮颜色。
我们将自动计算 hover 和 active 颜色。
<script lang="ts" setup>
import { isDark } from '~/composables/dark'
</script>
<template>
<div class="flex">
<el-button color="#626aef" :dark="isDark">Default</el-button>
<el-button color="#626aef" :dark="isDark" plain>Plain</el-button>
<el-button color="#626aef" :dark="isDark" disabled>Disabled</el-button>
<el-button color="#626aef" :dark="isDark" disabled plain
>Disabled Plain</el-button
>
</div>
</template>
Button 属性
属性 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
size | 尺寸 | string | large / default /small | — |
type | 类型 | string | primary / success / warning / danger / info / | — |
plain | 是否为朴素按钮 | boolean | — | false |
text2.2.0 | 是否为文字按钮 | boolean | — | false |
bg 2.2.0 | 是否显示文字按钮背景颜色 | boolean | — | false |
link 2.2.1 | 是否为链接按钮 | boolean | — | false |
round | 是否为圆角按钮 | boolean | — | false |
circle | 是否为圆形按钮 | boolean | — | false |
loading | 是否为加载中状态 | boolean | — | false |
loading-icon | 自定义加载中状态图标组件 | string | Component | — | Loading |
disabled | 按钮是否为禁用状态 | boolean | — | false |
icon | 图标组件 | string | Component | — | — |
autofocus | 原生 autofocus 属性 | boolean | — | false |
native-type | 原生 type 属性 | string | button / submit / reset | button |
auto-insert-space | 自动在两个中文字符之间插入空格 | boolean | — |
Button 插槽
插槽名 | 说明 |
---|---|
— | 自定义默认内容 |
loading | 自定义加载中组件 |
icon | 自定义图标组件 |
Button-Group 属性
属性 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
size | 用于控制该按钮组内按钮的大小 | string | 与按钮的大小相同 | — |
type | 用于控制该按钮组内按钮的类型 | string | 与按钮的类型一致 | — |
Button-Group 插槽
插槽名 | 说明 | 子标签 |
---|---|---|
- | 自定义按钮组内容 | Button |