Button 按钮

介绍

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

引入

通过以下方式来全局注册组件,更多注册方式请参考组件注册

  1. import { createApp } from 'vue';
  2. import { Button } from 'vant';
  3. const app = createApp();
  4. app.use(Button);

代码演示

按钮类型

按钮支持 defaultprimarysuccesswarningdanger 五种类型,默认为 default

  1. <van-button type="primary">主要按钮</van-button>
  2. <van-button type="success">成功按钮</van-button>
  3. <van-button type="default">默认按钮</van-button>
  4. <van-button type="warning">警告按钮</van-button>
  5. <van-button type="danger">危险按钮</van-button>

朴素按钮

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

  1. <van-button plain type="primary">朴素按钮</van-button>
  2. <van-button plain type="success">朴素按钮</van-button>

细边框

设置 hairline 属性可以展示 0.5px 的细边框。

  1. <van-button plain hairline type="primary">细边框按钮</van-button>
  2. <van-button plain hairline type="success">细边框按钮</van-button>

禁用状态

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

  1. <van-button disabled type="primary">禁用状态</van-button>
  2. <van-button disabled type="success">禁用状态</van-button>

加载状态

通过 loading 属性设置按钮为加载状态,加载状态下默认会隐藏按钮文字,可以通过 loading-text 设置加载状态下的文字。

  1. <van-button loading type="primary" />
  2. <van-button loading type="primary" loading-type="spinner" />
  3. <van-button loading type="success" loading-text="加载中..." />

按钮形状

通过 square 设置方形按钮,通过 round 设置圆形按钮。

  1. <van-button square type="primary">方形按钮</van-button>
  2. <van-button round type="success">圆形按钮</van-button>

图标按钮

通过 icon 属性设置按钮图标,支持 Icon 组件里的所有图标,也可以传入图标 URL。

  1. <van-button icon="plus" type="primary" />
  2. <van-button icon="plus" type="primary">按钮</van-button>
  3. <van-button
  4. icon="https://fastly.jsdelivr.net/npm/@vant/assets/user-active.png"
  5. type="primary"
  6. >
  7. 按钮
  8. </van-button>

按钮尺寸

支持 largenormalsmallmini 四种尺寸,默认为 normal

  1. <van-button type="primary" size="large">大号按钮</van-button>
  2. <van-button type="primary" size="normal">普通按钮</van-button>
  3. <van-button type="primary" size="small">小型按钮</van-button>
  4. <van-button type="primary" size="mini">迷你按钮</van-button>

块级元素

按钮在默认情况下为行内块级元素,通过 block 属性可以将按钮的元素类型设置为块级元素。

  1. <van-button type="primary" block>块级元素</van-button>

页面导航

可以通过 url 属性进行 URL 跳转,或通过 to 属性进行路由跳转。

  1. <van-button type="primary" url="https://github.com">URL 跳转</van-button>
  2. <van-button type="primary" to="index">路由跳转</van-button>

自定义颜色

通过 color 属性可以自定义按钮的颜色。

  1. <van-button color="#7232dd">单色按钮</van-button>
  2. <van-button color="#7232dd" plain>单色按钮</van-button>
  3. <van-button color="linear-gradient(to right, #ff6034, #ee0a24)">
  4. 渐变色按钮
  5. </van-button>

API

Props

参数说明类型默认值
type类型,可选值为 primary success warning dangerstringdefault
size尺寸,可选值为 large small ministringnormal
text按钮文字string-
color按钮颜色,支持传入 linear-gradient 渐变色string-
icon左侧图标名称或图片链接,等同于 Icon 组件的 name 属性string-
icon-prefix图标类名前缀,等同于 Icon 组件的 class-prefix 属性stringvan-icon
icon-position图标展示位置,可选值为 rightstringleft
tag按钮根节点的 HTML 标签stringbutton
native-type原生 button 标签的 type 属性stringbutton
block是否为块级元素booleanfalse
plain是否为朴素按钮booleanfalse
square是否为方形按钮booleanfalse
round是否为圆形按钮booleanfalse
disabled是否禁用按钮booleanfalse
hairline是否使用 0.5px 边框booleanfalse
loading是否显示为加载状态booleanfalse
loading-text加载状态提示文字string-
loading-type加载图标类型,可选值为 spinnerstringcircular
loading-size加载图标大小,默认单位为 pxnumber | string20px
url点击后跳转的链接地址string-
to点击后跳转的目标路由对象,等同于 vue-router 的 to 属性string | object-
replace是否在跳转时替换当前页面历史booleanfalse

Events

事件名说明回调参数
click点击按钮,且按钮状态不为加载或禁用时触发event: MouseEvent
touchstart开始触摸按钮时触发event: TouchEvent

Slots

名称说明
default按钮内容
icon v3.0.18自定义图标
loading自定义加载图标

类型定义

组件导出以下类型定义:

  1. import type {
  2. ButtonType,
  3. ButtonSize,
  4. ButtonProps,
  5. ButtonNativeType,
  6. ButtonIconPosition,
  7. } from 'vant';

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值描述
—van-button-mini-height24px-
—van-button-mini-padding0 var(—van-padding-base)-
—van-button-mini-font-sizevar(—van-font-size-xs)-
—van-button-small-height32px-
—van-button-small-padding0 var(—van-padding-xs)-
—van-button-small-font-sizevar(—van-font-size-sm)-
—van-button-normal-font-sizevar(—van-font-size-md)-
—van-button-normal-padding0 15px-
—van-button-large-height50px-
—van-button-default-height44px-
—van-button-default-line-height1.2-
—van-button-default-font-sizevar(—van-font-size-lg)-
—van-button-default-colorvar(—van-text-color)-
—van-button-default-backgroundvar(—van-background-2)-
—van-button-default-border-colorvar(—van-gray-4)-
—van-button-primary-colorvar(—van-white)-
—van-button-primary-backgroundvar(—van-primary-color)-
—van-button-primary-border-colorvar(—van-primary-color)-
—van-button-success-colorvar(—van-white)-
—van-button-success-backgroundvar(—van-success-color)-
—van-button-success-border-colorvar(—van-success-color)-
—van-button-danger-colorvar(—van-white)-
—van-button-danger-backgroundvar(—van-danger-color)-
—van-button-danger-border-colorvar(—van-danger-color)-
—van-button-warning-colorvar(—van-white)-
—van-button-warning-backgroundvar(—van-orange)-
—van-button-warning-border-colorvar(—van-orange)-
—van-button-border-widthvar(—van-border-width)-
—van-button-radiusvar(—van-radius-md)-
—van-button-round-radiusvar(—van-radius-max)-
—van-button-plain-backgroundvar(—van-white)-
—van-button-disabled-opacityvar(—van-disabled-opacity)-
—van-button-icon-size1.2em-
—van-button-loading-icon-size20px-

Button 按钮 - 图1