# button

按钮。

属性名 类型 默认值 说明 生效时机 最低版本
size String default 按钮的大小
type String default 按钮的样式类型
plain Boolean false 按钮是否镂空,背景色透明
disabled Boolean false 是否禁用
loading Boolean false 名称前是否带 loading 图标
form-type String 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
open-type String QQ 开放能力
hover-class String button-hover 指定按钮按下去的样式类。当 hover-class="none" 时,没有点击态效果
hover-stop-propagation Boolean false 指定是否阻止本节点的祖先节点出现点击态
hover-start-time Number 20 按住后多久出现点击态,单位毫秒
hover-stay-time Number 70 手指松开后点击态保留时间,单位毫秒
lang String en 指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文。 open-type="getUserInfo"
bindgetuserinfo Handler 用户点击该按钮时,会返回获取到的用户信息,回调的 detail 数据与qq.getUserInfo返回的一致 open-type="getUserInfo"
app-parameter String 打开 APP 时,向 APP 传递的参数 open-type="launchApp"
group-id String 打开群资料卡时,传递的群号 open-type="openGroupProfile"
guild-id String 打开频道页面时,传递的频道号 open-type="openGuildProfile" 1.46.8
public-id String 打开公众号资料卡时,传递的号码 open-type="openPublicProfile" 1.12.0
binderror Handler 当使用开放能力时,发生错误的回调 open-type="launchApp"
bindlaunchapp Handler 打开 APP 成功的回调 open-type="launchApp"
bindopensetting Handler 在打开授权设置页后回调 open-type="openSetting"
share-type Number 27 分享类型集合,请参考下面share-type有效值说明。share-type后续将不再维护,请更新为share-mode open-type="share" 1.4.4
share-mode Array<String> ['qq', 'qzone'] 分享类型集合,请参考下面share-mode有效值说明 open-type="share" 1.15.0
aria-label String 无障碍访问,(属性)元素的额外描述
open-id String 添加好友时,对方的 openid open-type="addFriend"
bindaddfriend Handler 添加好友的回调 open-type="addFriend"
bindaddgroupapp Handler 添加群应用的回调。errCode 错误码:41004(当前用户非管理员或群主,无权操作),41005(超过可添加群应用的群数量) open-type="addGroupApp" 1.16.0
bindcontact Handler 打开客服会话的回调。 open-type="contact"
share-message-friend-info FriendInfo 发送对象的 FriendInfo open-type="shareMessageToFriend" 1.17.0
share-message-title String 转发标题,不传则默认使用当前小程序的昵称。 FriendInfo open-type="shareMessageToFriend" 1.17.0
share-message-img String 转发显示图片的链接,可以是网络图片路径(仅 QQ CDN 域名路径)或本地图片文件路径或相对代码包根目录的图片文件路径。显示图片长宽比是 5:4FriendInfo open-type="shareMessageToFriend" 1.17.0
  • 注 1:button-hover 默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;}
  • 注 2:button 可作为原生组件的子节点嵌入,以便在原生组件上使用 open-type 的能力

size 有效值:

说明
default 默认大小
mini 小尺寸

type 有效值:

说明
primary 绿色
default 白色
warn 红色

form-type 有效值:

说明
submit 提交表单
reset 重置表单

open-type 有效值:

说明 最低版本
share 触发用户转发,使用前建议先阅读使用指引
getUserInfo 获取用户信息,可以从 bindgetuserinfo 回调中获取到用户信息
launchApp 打开 APP,可以通过 app-parameter 属性设定向 APP 传的参数具体说明 暂时以邀请制开放申请,请留意后续通知
openSetting 打开授权设置页
feedback 呼起吐个槽反馈页面,开发者可以到官网查看反馈 基础库 1.2.0 版本开始支持
openGroupProfile 呼起群资料卡页面,可以通过 group-id 属性设定需要打开的群资料卡的群号,同时 app.json 中必须配置 groupIdList(数量不超过 10 个),表明可以打开群资料卡的群号 基础库 1.4.7 版本开始支持
openGuildProfile 呼起频道页面,可以通过 guild-id 属性设定需要打开的频道ID(每个小程序能呼起的频道数量上限为5个),频道 ID 获取方式请联系 qq-miniprogram@tencent.com,邮件内容包括小程序名称、appid、频道主QQ号、频道名称、频道号 基础库 1.46.8 版本开始支持
contact 打开客服会话,需要在管理端申请开通,目前功能正在内测中,满足内测资格即可在开发者管理端看到开通入口,客服收发消息能力描述参考
addFriend 添加好友, 对方需要通过该小程序进行授权,允许被加好友后才能调用成功用户授权
addColorSign 添加彩签,点击后添加状态有用户提示,无回调 基础库 1.10.0 版本开始支持
openPublicProfile 打开公众号资料卡,可以通过 public-id 属性设定需要打开的公众号资料卡的号码,同时 app.json 中必须配置 publicIdList(目前只能配置 1 个),表明可以打开的公众号资料卡的号码 基础库 1.12.0 版本开始支持
addGroupApp 添加群应用(只有管理员或群主有权操作,建议先调用 qq.getGroupInfo 获取当前用户是否为管理员,如果是管理员才显示该按钮),添加后给button绑定bindaddgroupapp事件接收回调数据。 基础库 1.16.0 版本开始支持
shareMessageToFriend 在自定义开放数据域组件中,向指定好友发起分享据 基础库 1.17.0 版本开始支持
addToFavorites 收藏当前页面,点击按钮后会触发 Page.onAddToFavorites 方法 基础库 1.19.0 版本开始支持

share-type 有效值:

说明: share-type后续将不再维护,请更新为share-modeshare-typeshare-mode同时使用,优先采用share-mode的值

字段值按字节bit位分配

bit低位到高位:

  1. 第 1 位bit0为分享到好友
  2. 第 2 位bit1为分享到空间
  3. 第 3 位bit2为快速分享到当前聊天窗口
  4. 第 4 位bit3为分享到微信好友
  5. 第 5 位bit4为分享到微信朋友圈
  6. 第 6 位bit5为分享到最近的联系人列表

bit位优先级:

bit2 > bit5 > bit0bit1bit3bit4

比如share-type设置为 7(二进制表示为 111),则为快速分享到当前聊天窗口,分享到好友和分享到空间会被忽略。

bit 位 说明 最低版本
1 分享面板展示“分享到好友” 1.4.4
2 分享面板展示“分享到空间” 1.4.4
3 进行快速分享,直接分享到当前聊天窗口,page.onShareAppMessage 需要带上 entryDataHash 参数 1.6.3
4 分享面板展示“分享到微信好友” 1.6.3
5 分享面板展示“分享到微信朋友圈” 1.6.3
6 分享到最近的联系人列表 1.8.5

share-mode 数组元素的有效值:

说明: share-typeshare-mode同时使用,优先采用share-mode的值

元素值 说明 最低版本
'qq' 分享面板展示“分享到好友” 1.15.0
'qzone' 分享面板展示“分享到空间” 1.15.0
'quickToDialog' 进行快速分享,直接分享到当前聊天窗口,page.onShareAppMessage 需要带上 entryDataHash 参数 1.15.0
'wechatFriends' 分享面板展示“分享到微信好友” 1.15.0
'wechatMoment' 分享面板展示“分享到微信朋友圈” 1.15.0
'recentContacts' 分享到最近的联系人列表 1.15.0

元素值的优先级:

"quickToDialog" > "recentContacts" > ("qq" = "qzone" = "wechatFriends" = "wechatMoment")

比如 share-mode 设置为['qq', 'qzone', 'quickToDialog'],则为快速分享到当前聊天窗口,分享到好友和分享到空间会被忽略。

示例代码:

/** qss **/
/** 修改button默认的点击态样式类**/
.button-hover {
  background-color: red;
}
/** 添加自定义button点击态样式类**/
.other-button-hover {
  background-color: blue;
}
1
2
3
4
5
6
7
8
9
<button
  type="default"
  size="{{defaultSize}}"
  loading="{{loading}}"
  plain="{{plain}}"
  disabled="{{disabled}}"
  bindtap="default"
  hover-class="other-button-hover"
>
  default
</button>
<button
  type="primary"
  size="{{primarySize}}"
  loading="{{loading}}"
  plain="{{plain}}"
  disabled="{{disabled}}"
  bindtap="primary"
>
  primary
</button>
<button
  type="warn"
  size="{{warnSize}}"
  loading="{{loading}}"
  plain="{{plain}}"
  disabled="{{disabled}}"
  bindtap="warn"
>
  warn
</button>
<button bindtap="setDisabled">点击设置以上按钮disabled属性</button>
<button bindtap="setPlain">点击设置以上按钮plain属性</button>
<button bindtap="setLoading">点击设置以上按钮loading属性</button>
<button open-type="contact" bindcontact="onEnterContact">进入客服会话</button>
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">
  获取用户信息
</button>
<button open-type="openSetting">打开授权设置页</button>
<button open-type="share" share-type="{{1}}">只能分享到好友</button>
<button open-type="share" share-type="{{2}}">只能分享到空间</button>
<button open-type="share" share-type="{{3}}">可以分享到好友和空间</button>
<button open-type="share" share-type="{{4}}">进行快速分享</button>
<button open-type="share" share-type="{{32}}">分享到最近的联系人</button>
<button open-type="share" share-mode="{{['qq']}}">只能分享到好友</button>
<button open-type="share" share-mode="{{['qzone']}}">只能分享到空间</button>
<button open-type="share" share-mode="{{['qq', 'qzone']}}">
  可以分享到好友和空间
</button>
<button open-type="openGroupProfile" group-id="123456">打开群资料卡</button>
<button open-type="openGuildProfile" guild-id="123456">打开频道页面</button>
<button open-type="openPublicProfile" public-id="123456">
  打开公众号资料卡
</button>
<button open-type="addGroupApp" bindaddgroupapp="addgroupapp">
  添加群应用
</button>
<button open-type="addToFavorites">添加到收藏</button>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const types = ["default", "primary", "warn"];
const pageObject = {
  data: {
    defaultSize: "default",
    primarySize: "default",
    warnSize: "default",
    disabled: false,
    plain: false,
    loading: false,
  },
  setDisabled(e) {
    this.setData({
      disabled: !this.data.disabled,
    });
  },
  setPlain(e) {
    this.setData({
      plain: !this.data.plain,
    });
  },
  setLoading(e) {
    this.setData({
      loading: !this.data.loading,
    });
  },
  onGotUserInfo(e) {
    console.log(e.detail.errMsg);
    console.log(e.detail.userInfo);
    console.log(e.detail.rawData);
  },
  // 添加群应用的回调事件
  addgroupapp(e) {
    console.log(e.detail.errMsg);
  },
};

for (let i = 0; i < types.length; ++i) {
  (function(type) {
    pageObject[type] = function(e) {
      const key = type + "Size";
      const changedData = {};
      changedData[key] = this.data[key] === "default" ? "mini" : "default";
      this.setData(changedData);
    };
  })(types[i]);
}

Page(pageObject);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/** app.json **/
/** 必须要配置groupIdList,且group-id必须在groupIdList中才能生效**/
/** 必须要配置publicIdList,且public-id必须在publicIdList中才能生效**/
{
  "pages": ["pages/index/index", "pages/logs/index"],
  "window": {
    "navigationBarTitleText": "Demo"
  },
  "tabBar": {
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "日志"
      }
    ]
  },
  "networkTimeout": {
    "request": 10000,
    "downloadFile": 10000
  },
  "debug": true,
  "navigateToMiniProgramAppIdList": ["qqe5f52902cf4de896"],
  "groupIdList": ["123456", "34356576", "457658769"],
  "publicIdList": ["123456"]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

button

# Bug & Tip
  1. tip: 目前,设置了 form-typebutton 只会对当前组件中的 form 有效。因而,将 button 封装在自定义组件中,而 from 在自定义组件外,将会使这个 buttonform-type 失效。