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

改成这样就可以了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注