在whtml中这么写
<view bindtap='like' data-id='{{laugh.id}}' data-createdBy='{{laugh.created_by}}'>
在js中取值
like(e){ console.log(e.currentTarget.dataset['createdBy']);}
控制台输出undefined,找了半天原因,后来网上查询了类似相同问题下才知道
在组件中可以定义数据,这些数据将会通过事件传递给 SERVICE。 书写方式: 以data-开头,多个单词由连字符-链接,不能有大写(大写会自动转成小写)如data-element-type,最终在 event.currentTarget.dataset 中会将连字符转成驼峰elementType。
原来小程序自动转了,会把下划线转为大写,大写转为小写(坑爹啊。。)
like(e){ console.log(e.currentTarget.dataset['createdBy']);}
改成这样就可以了。
本站部分原创文章,部分文章整理自网络。如有转载的文章侵犯了您的版权,请联系站长删除处理。
联系客服