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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_19671783/article/details/82866255

在whtml中这么写

<view bindtap='like' data-id='{{laugh.id}}' data-createdBy='{{laugh.created_by}}'>

在js中取值

like(e){
    console.log(e.currentTarget.dataset['createdBy']);
 }

控制台输出undefined,找了半天原因,后台在https://www.cnblogs.com/dawenyang/p/9039495.html 这篇博客中找到原因:

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

原来小程序自动转了,会把下划线转为大写,大写转为小写(坑爹啊。。)

like(e){
    console.log(e.currentTarget.dataset['createdby']);
 }

改成这样就可以了。

猜你喜欢

转载自blog.csdn.net/sinat_19671783/article/details/82866255
今日推荐