2-3 button组件

button

基础库 1.0.0 开始支持,低版本需做兼容处理

按钮。

属性类型默认值必填说明最低版本
sizestringdefault按钮的大小1.0.0
typestringdefault按钮的样式类型1.0.0
plainbooleanfalse按钮是否镂空,背景色透明1.0.0
disabledbooleanfalse是否禁用1.0.0
loadingbooleanfalse名称前是否带 loading 图标1.0.0
form-typestring 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件1.0.0
open-typestring 微信开放能力1.1.0
hover-classstringbutton-hover指定按钮按下去的样式类。当 hover-class="none" 时,没有点击态效果1.0.0
hover-stop-propagationbooleanfalse指定是否阻止本节点的祖先节点出现点击态1.5.0
hover-start-timenumber20按住后多久出现点击态,单位毫秒1.0.0
hover-stay-timenumber70手指松开后点击态保留时间,单位毫秒1.0.0
langstringen指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文。1.3.0
session-fromstring 会话来源,open-type="contact"时有效1.4.0
send-message-titlestring当前标题会话内消息卡片标题,open-type="contact"时有效1.5.0
send-message-pathstring当前分享路径会话内消息卡片点击跳转小程序路径,open-type="contact"时有效1.5.0
send-message-imgstring截图会话内消息卡片图片,open-type="contact"时有效1.5.0
app-parameterstring 打开 APP 时,向 APP 传递的参数,open-type=launchApp时有效1.9.5
show-message-cardbooleanfalse是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息,open-type="contact"时有效1.5.0
bindgetuserinfoeventhandle 用户点击该按钮时,会返回获取到的用户信息,回调的detail数据与wx.getUserInfo返回的一致,open-type="getUserInfo"时有效1.3.0
bindcontacteventhandle 客服消息回调,open-type="contact"时有效1.5.0
bindgetphonenumbereventhandle 获取用户手机号回调,open-type=getPhoneNumber时有效1.2.0
binderroreventhandle 当使用开放能力时,发生错误的回调,open-type=launchApp时有效1.9.5
bindopensettingeventhandle 在打开授权设置页后回调,open-type=openSetting时有效2.0.7
bindlaunchappeventhandle 打开 APP 成功的回调,open-type=launchApp时有效2.4.4

size 的合法值

说明最低版本
default默认大小 
mini小尺寸 

type 的合法值

说明最低版本
primary绿色 
default白色 
warn红色 

form-type 的合法值

说明最低版本
submit提交表单 
reset重置表单 

open-type 的合法值

说明最低版本
contact打开客服会话,如果用户在会话中点击消息卡片后返回小程序,可以从 bindcontact 回调中获得具体信息,具体说明1.1.0
share触发用户转发,使用前建议先阅读使用指引1.2.0
getPhoneNumber获取用户手机号,可以从bindgetphonenumber回调中获取到用户信息,具体说明1.2.0
getUserInfo获取用户信息,可以从bindgetuserinfo回调中获取到用户信息1.3.0
launchApp打开APP,可以通过app-parameter属性设定向APP传的参数具体说明1.9.5
openSetting打开授权设置页2.0.7
feedback打开“意见反馈”页面,用户可提交反馈内容并上传日志,开发者可以登录小程序管理后台后进入左侧菜单“客服反馈”页面获取到反馈内容2.1.0

lang 的合法值

说明最低版本
en英文 
zh_CN简体中文 
zh_TW繁体中文 

Bug & Tip

  1. tipbutton-hover 默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;}
  2. tipbindgetphonenumber 从1.2.0 开始支持,但是在1.5.3以下版本中无法使用wx.canIUse进行检测,建议使用基础库版本进行判断。
  3. tip: 在bindgetphonenumber 等返回加密信息的回调中调用 wx.login 登录,可能会刷新登录态。此时服务器使用 code 换取的 sessionKey 不是加密时使用的 sessionKey,导致解密失败。建议开发者提前进行 login;或者在回调中先使用 checkSession 进行登录态检查,避免 login 刷新登录态。
  4. tip: 从 2.1.0 起,button 可作为原生组件的子节点嵌入,以便在原生组件上使用 open-type 的能力。
  5. tip: 目前设置了 form-type 的 button 只会对当前组件中的 form 有效。因而,将 button 封装在自定义组件中,而 from 在自定义组件外,将会使这个 button 的 form-type 失效。

示例代码

在开发者工具中预览效果

.button-hover {
  background-color: red;
}
.other-button-hover {
  background-color: blue;
}
<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">进入客服会话</button>
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">
  获取用户信息
</button>
<button open-type="openSetting">打开授权设置页</button>
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)
  },
//最上面3个按钮事件触发可以改为
    default(e) {
    if (this.data.defaultSize == 'default'){
      this.setData({
        defaultSize: 'mini'
      })
    }else{
      this.setData({
        defaultSize: 'default'
      })
    }
  },
//又或者
primary(e) {
    this.setData({
      primarySize: (this.data.primarySize == 'default') ? 'mini' : 'default'
    })
  },
  warn(e) {
    this.setData({
      warnSize: (this.data.warnSize == 'default') ? 'mini' : 'default'
    })
  }
}

//最上面3个按钮的事件触发
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)

以上内容来自文档:https://developers.weixin.qq.com/miniprogram/dev/component/button.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值