Skip to content

Button 按钮

介绍

按钮用于触发一个操作,如提交表单。

按钮类型

通过 type 属性设置按钮类型,支持 defaultinfoprimarysuccesswarningdanger 六种类型,默认类型为 default

html
<vd-button type="default">默认按钮</vd-button>
<vd-button type="info">信息按钮</vd-button>
<vd-button type="primary">主要按钮</vd-button>
<vd-button type="success">成功按钮</vd-button>
<vd-button type="warning">警告按钮</vd-button>
<vd-button type="danger">危险按钮</vd-button>

朴素按钮

通过 plain 属性将按钮设置为朴素按钮,朴素按钮的文字为按钮颜色,背景为透明。

html
<vd-button type="success" plain>成功按钮</vd-button>

禁用状态

通过 disabled 属性来禁用按钮,禁用状态下按钮不可点击。

html
<vd-button type="success" disabled>成功按钮</vd-button>

加载样式

支持 prefixsuffixfullmask 四种加载样式,默认为 prefix

html
<vd-button type="success" :loading="true" loading-style="prefix">成功按钮</vd-button>

加载类型

加载类型参考 Loading 组件。

html
<vd-button type="success" :loading="true" loading-type="spinner">成功按钮</vd-button>

加载状态

可以通过 loading 属性维护加载中状态,也可通过 action 属性传入一个 promise 方法,点击后会执行该 promise ,在进入 FulfilledRejected 状态后自动结束加载状态。

html
<vd-button type="success" :loading="true">成功按钮</vd-button>
<vd-button type="success" :action="handleSubmit">成功按钮</vd-button>
<vd-button type="success" :action="handleSubmit2">成功按钮</vd-button>
js
import { timeout } from '@vital-design/shared';

async function handleSubmit() {
  await timeout(2000); // 模拟 2000ms 请求
  console.log('提交成功');
}

function handleSubmit2() {
  return new Promise((resolve) => {
    setTimeout(() => { // 模拟 2000ms 请求
      resolve({ code: 1, msg: '提交成功' });
      console.log('提交成功');
    }, 2000);
  });
}

链接按钮

通过 link 属性将按钮设置为链接按钮。

html
<vd-button type="success" link>链接按钮</vd-button>

块级按钮

通过 block 属性将按钮设置为块级按钮。

html
<vd-button type="success" block>块级按钮</vd-button>

圆角按钮

通过 round 属性将按钮设置为圆角按钮。

html
<vd-button type="success" round>圆角按钮</vd-button>

按钮图标

按钮图标参考 Icon 组件。

html
<vd-button type="success" icon="star">带图标按钮</vd-button>

API

Props

参数说明类型默认值
text按钮文本string-
type按钮类型ButtonType-
form-type表单类型ButtonFormType-
link是否链接按钮booleanfalse
plain是否朴素按钮booleanfalse
block是否块级按钮booleanfalse
round是否圆角按钮booleanfalse
disabled是否禁用按钮booleanfalse
loading是否加载中booleanfalse
loading-type加载中类型LoadingType'spinner'
loading-style加载中样式ButtonLoadingStyle'prefix'
action按钮操作事件Function-
icon按钮图标IconsName-
icon-size按钮图标大小number | string-

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Events

事件名说明回调参数
click点击时触发MouseEvent

TIP

按钮基于 button 组件,更多 props/events 可前往 uniapp 官网 Button 查看。

Slots

名称说明
default自定义内容
icon图标
loading加载中

类型定义

组件导出以下类型定义:

ts
import type { ButtonType, ButtonLoadingStyle } from 'vital-design';

赣ICP备2025061025号-1