Skip to content

Button 按钮 ​

uvx-button 提供主题、尺寸、形状、加载、禁用、图标、自定义颜色和平台开放能力。

平台兼容性 ​

AndroidiOS鸿蒙H5微信
3.9+4.11+4.61+4.0+4.41+

小程序端仅支持微信。

平台能力 ​

能力AndroidiOS鸿蒙H5微信
主题、尺寸、形状、图标、加载、禁用√√√√√
click 点击事件与节流√√√√√
form-type 表单提交与重置√√√√√
open-type="agreePrivacyAuthorization"4.31+4.31+4.61+×√
其他 open-type 开放能力××××√
微信开放能力属性与回调事件××××√

使用须知

  1. 点击事件请使用 @click。disabled 或 loading 状态下不会触发点击,throttle-time 可设置点击节流时间。
  2. App 端的 open-type 仅支持 agreePrivacyAuthorization;H5 不传递 open-type;其他开放能力仅微信小程序支持。
  3. hairline 使用 0.5px 细边框。color 为渐变色时组件会移除边框,hairline 不生效。
  4. icon 使用 uvx-icon 的图标名称;默认插槽会替换 text 内容,suffix 插槽追加在文字后方。

基础用法 ​

使用 type 设置按钮主题,支持 info、primary、success、warning 和 error。

vue
<template>
   <view class="button-list">
      <uvx-button text="默认按钮" type="info"></uvx-button>
      <uvx-button text="主要按钮" type="primary"></uvx-button>
      <uvx-button text="成功按钮" type="success"></uvx-button>
      <uvx-button text="警告按钮" type="warning"></uvx-button>
      <uvx-button text="危险按钮" type="error"></uvx-button>
   </view>
</template>

镂空与细边框 ​

plain 使用当前主题的容器背景,并将边框和文字切换为主题色。hairline 控制是否使用 0.5px 细边框,关闭后恢复普通 1px 边框。

vue
<template>
   <view class="button-list">
      <uvx-button
         text="镂空按钮"
         type="primary"
         :plain="true"
      ></uvx-button>
      <uvx-button
         text="普通边框"
         type="warning"
         :plain="true"
         :hairline="false"
      ></uvx-button>
   </view>
</template>

禁用与加载 ​

disabled 会降低按钮透明度并阻止点击。loading 会隐藏普通内容,显示加载图标和 loading-text,同时阻止点击。

vue
<template>
   <view class="button-list">
      <uvx-button
         text="禁用按钮"
         type="primary"
         :disabled="true"
      ></uvx-button>
      <uvx-button
         type="success"
         loading-text="提交中"
         loading-mode="circle"
         :loading="true"
      ></uvx-button>
   </view>
</template>

图标与形状 ​

icon 设置前置图标,shape="circle" 设置胶囊圆角。图标默认跟随按钮文字颜色和尺寸,也可以使用 icon-color、icon-size 单独覆盖。

vue
<template>
   <view class="button-list">
      <uvx-button
         text="定位"
         type="warning"
         icon="map"
         :plain="true"
      ></uvx-button>
      <uvx-button
         text="圆角按钮"
         type="success"
         shape="circle"
      ></uvx-button>
   </view>
</template>

自定义颜色 ​

color 可传普通 CSS 颜色或 linear-gradient 渐变。渐变色用于实心按钮时会移除边框。

vue
<template>
   <view class="button-list">
      <uvx-button
         text="自定义颜色"
         color="rgb(10, 185, 156)"
      ></uvx-button>
      <uvx-button
         text="渐变色按钮"
         color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))"
      ></uvx-button>
   </view>
</template>

按钮尺寸 ​

size 支持 large、normal、small、mini,默认值为 normal。

vue
<template>
   <view class="button-list">
      <uvx-button text="大型按钮" type="success" size="large"></uvx-button>
      <uvx-button text="普通按钮" type="error" size="normal"></uvx-button>
      <uvx-button text="小型按钮" type="primary" size="small"></uvx-button>
      <uvx-button text="迷你按钮" type="warning" size="mini"></uvx-button>
   </view>
</template>

自定义样式 ​

u-style 设置按钮根节点样式,u-text-style 设置文字样式,均使用 CSS 字符串。u-class 可追加外部样式类。

vue
<template>
   <uvx-button
      text="自定义按钮"
      type="primary"
      u-class="submit-button"
      u-style="width: 100%; height: 48px; border-radius: 24px;"
      u-text-style="font-size: 16px; font-weight: 600;"
   ></uvx-button>
</template>

微信开放能力 ​

微信小程序通过 open-type 使用原生开放能力,组件会透传对应回调。以下示例使用 getPhoneNumber 获取用户手机号:

vue
<template>
   <uvx-button
      text="获取手机号"
      type="primary"
      open-type="getPhoneNumber"
      @getphonenumber="handlePhoneNumber"
   ></uvx-button>
</template>

<script lang="uts" setup>
/**
 * 处理获取手机号回调
 * @param event 微信原生按钮事件
 * @returns null
 */
const handlePhoneNumber = (event: UniEvent): void => {
   console.log(event.detail);
};
</script>

开放能力需要微信小程序账号权限、基础库版本和对应业务配置。getPhoneNumber 与 getRealtimePhoneNumber 均为按次计费的手机号验证能力,收到回调后应立即隐藏或禁用按钮,避免重复授权产生额外费用。

API ​

Props ​

属性说明类型默认值平台
type按钮主题info | primary | error | warning | successinfo全部
size按钮尺寸large | normal | small | mininormal全部
shape按钮形状square | circlesquare全部
plain是否使用镂空样式booleanfalse全部
hairline是否使用 0.5px 细边框,渐变色下不生效booleantrue全部
disabled是否禁用按钮booleanfalse全部
loading是否显示加载状态并阻止点击booleanfalse全部
loading-text加载状态文字,空字符串时回退到 textstring加载中全部
loading-mode加载图标模式spinner | circle | semicirclespinner全部
loading-size加载图标尺寸,单位 pxnumber15全部
text按钮文字string | number""全部
icon前置图标名称string""全部
icon-size图标尺寸,数字默认补充 pxstring | number""全部
icon-color图标颜色;未设置时跟随按钮状态string""全部
color自定义颜色,支持普通颜色和 linear-gradientstring""全部
form-type触发所属 form 的提交或重置submit | reset | """"全部
open-type平台开放能力,取值范围见下表OpenType""App、微信
app-parameterlaunchApp 时传递给 App 的参数string""微信
hover-stop-propagation是否阻止祖先节点出现点击态booleantrue微信
hover-start-time按住后出现点击态的时间,单位 msstring | number0全部
hover-stay-time松开后保留点击态的时间,单位 msstring | number200全部
lang返回用户信息的语言zh_CN | zh_TW | enzh_CN微信
session-fromcontact 客服会话来源string""微信
send-message-title客服会话内消息卡片标题string""微信
send-message-path客服会话内消息卡片跳转路径string""微信
send-message-img客服会话内消息卡片图片string""微信
show-message-card是否显示客服会话消息卡片booleanfalse微信
phone-number-no-quota-toast手机号验证额度用尽时是否显示提示booleantrue微信
throttle-time点击事件节流时间,单位 msnumber0全部
u-style按钮根节点样式,CSS 字符串string""全部
u-text-style按钮文字样式,CSS 字符串string""全部
u-class追加到按钮根节点的外部样式类string""全部

OpenType ​

平台支持值
AndroidagreePrivacyAuthorization(HBuilderX 4.31+)
iOSagreePrivacyAuthorization(HBuilderX 4.31+)
鸿蒙agreePrivacyAuthorization(HBuilderX 4.61+)
H5不支持
微信feedback、share、getUserInfo、contact、getPhoneNumber、getRealtimePhoneNumber、launchApp、openSetting、chooseAvatar、agreePrivacyAuthorization、liveActivity

Events ​

微信开放能力事件的参数均为原生 UniEvent,具体数据从 event.detail 获取。

事件说明参数平台
click按钮点击;禁用或加载中不触发-全部
getphonenumber获取手机号,open-type="getPhoneNumber" 时触发UniEvent微信
getrealtimephonenumber实时验证手机号,open-type="getRealtimePhoneNumber" 时触发UniEvent微信
getuserinfo获取用户信息,open-type="getUserInfo" 时触发UniEvent微信
contact客服会话回调,open-type="contact" 时触发UniEvent微信
chooseavatar选择头像,open-type="chooseAvatar" 时触发UniEvent微信
agreeprivacyauthorization同意隐私协议,open-type="agreePrivacyAuthorization" 时触发UniEvent微信
opensetting打开授权设置页后的回调UniEvent微信
launchapp打开 App 成功的回调UniEvent微信
error使用开放能力发生错误的回调UniEvent微信
createliveactivity一次性订阅消息下发机制回调,获取下发 code,open-type="liveActivity" 时触发UniEvent微信

Slots ​

名称说明平台
default自定义按钮主体内容;使用后替换 text全部
suffix在按钮主体内容后追加内容全部

参考 ​

uvx-ui · uni-app x 组件库