bind:tap="onLike" 中不能添加参数 及 dataset 传值

本文详细解析了微信小程序中bindtap事件如何正确传递参数,避免常见的'onLike(id)'函数调用错误,介绍了data-value属性的使用方法,并通过实例展示了如何在组件中定义数据并传递给页面。
<view bind:tap="onLike({{id}})"></view>

//Component "pages/book-detail/book-detail" does not have a method "onLike(7)" to handle event "tap".

//bind:tap 中函数如果加参数,小程序会默认寻找 onLike(7)(){}这个函数

//bind:tap 引号中的内容全部看出函数名

 

如果需要传值,组件中用data-value

在组件中可以定义数据,这些数据将会通过事件传递给 SERVICE。 书写方式: 以data-开头,多个单词由连字符-链接,不能有大写(大写会自动转成小写)如data-element-type,最终在 event.currentTarget.dataset 中会将连字符转成驼峰elementType。

 

示例:

<view data-alpha-beta="1" data-alphaBeta="2" bindtap="bindViewTap">
  DataSet Test
</view>

 

Page({
  bindViewTap(event) {
    event.currentTarget.dataset.alphaBeta === 1 // - 会转为驼峰写法
    event.currentTarget.dataset.alphabeta === 2 // 大写会转为小写
  }
})

 

转载于:https://www.cnblogs.com/qq254980080/p/10681203.html

<!-- 新增错误提示样式 --> <van-popup show="{{ show }}" position="bottom" custom-class="custom-form-modal" close-on-click-overlay="{{ false }}" bind:close="closeModal"> <!-- 新增加载状态 --> <van-overlay show="{{ loading }}" /> <van-loading wx:if="{{ loading }}" custom-class="loading-overlay" size="24px" vertical>加载中...</van-loading> <view class="modal-header"> <view class="title">{{ title }}</view> <van-icon name="close" size="20px" bind:tap="closeModal" /> </view> <scroll-view scroll-y class="form-content"> <block wx:if="{{!loading && config.length}}"> <block wx:for="{{ config }}" wx:key="index"> <view class="form-row"> <view class="row-title">{{ item.title }}</view> <view class="row-content"> <block wx:for="{{ item.items }}" wx:key="field"> <!-- 新增错误提示 --> <view wx:if="{{ errors[subItem.field] }}" class="error-message"> {{ errors[subItem.field] }} </view> <!-- 输入框 --> <block wx:if="{{ subItem.type === 'input' }}"> <van-field value="{{ formData[subItem.field] }}" placeholder="{{ subItem.placeholder || '请输入' }}" data-field="{{ subItem.field }}" bind:change="handleInput" border="{{ false }}" /> </block> <!-- 时间选择器 --> <block wx:elif="{{ subItem.type === 'time' }}"> <view class="time-picker {{ errors[subItem.field] ? 'error' : '' }}" bindtap="openTimePicker" data-field="{{ subItem.field }}"> {{ formData[subItem.field] || subItem.placeholder || '选择时间' }} </view> <van-popup show="{{ tempData[subItem.field + '_show'] }}" position="bottom" data-field="{{ subItem.field }}" bind:close="closeTimePicker"> <van-datetime-picker type="time" value="{{ formData[subItem.field] }}" data-field="{{ subItem.field }}" bind:confirm="handleTimeChange" bind:cancel="closeTimePicker" /> </van-popup> </block> <!-- 下拉选择器 --> <block wx:elif="{{ subItem.type === 'select' }}"> <view class="select-picker {{ errors[subItem.field] ? 'error' : '' }}" bindtap="openSelectPicker" data-field="{{ subItem.field }}"> {{ getSelectLabel(subItem.options, formData[subItem.field]) || subItem.placeholder || '请选择' }} </view> <van-popup show="{{ tempData[subItem.field + '_show'] }}" position="bottom" data-field="{{ subItem.field }}" bind:close="closeSelectPicker"> <van-picker show-toolbar columns="{{ subItem.options }}" value-key="label" data-field="{{ subItem.field }}" bind:confirm="handleSelectChange" bind:cancel="closeSelectPicker" /> </van-popup> </block> <!-- 按钮 --> <block wx:elif="{{ subItem.type === 'button' }}"> <van-button type="primary" size="small" data-field="{{ subItem.field }}" bind:tap="handleButtonClick"> {{ subItem.text }} </van-button> </block> <!-- 自定义组件 --> <block wx:elif="{{ subItem.type === 'custom' }}"> <custom-component value="{{ formData[subItem.field] }}" config="{{ subItem.config }}" data-field="{{ subItem.field }}" bind:event="handleCustomEvent" /> </block> </block> </view> </view> </block> </block> </scroll-view> <view class="modal-footer"> <van-button type="default" size="large" bind:tap="closeModal">取消</van-button> <van-button type="primary" size="large" bind:tap="submitForm">提交</van-button> </view> </van-popup> 第二个for循环数据错误
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值