微信小程序data-绑定数据的坑

来源:网络 文章列表 2020-06-09 8
在whtml中这么写 <view bindtap='like' data-id='{{laugh.id}}' data-createdBy='{{laugh.created_by}}'> 在js中取值 like(e){ console.log(e.currentTarget.dataset['createdBy']); } 控制台输出undefined,找了半天原因,

在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']);
}

改成这样就可以了。

腾讯云限量秒杀

1核2G 5M 50元/年 2核4G 8M 74元/年 4核8G 5M 818元/年 CDN流量包 100GB 9元

版权声明

本站部分原创文章,部分文章整理自网络。如有转载的文章侵犯了您的版权,请联系站长删除处理。如果您有优质文章,欢迎发稿给我们!联系站长:
愿本站的内容能为您的学习、工作带来绵薄之力。

评论

  • 随机获取
点击刷新
精彩评论

友情链接