「小程序JAVA实战」 小程序的事件(11)
2018-11-28 本文已影响3人
IT人故事会
原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
原文链接地址:「小程序JAVA实战」 小程序的事件(11)
我们以前在web开发的时候,web页面也有一些相关的事件,当然小程序要接触屏幕要进行一些点击和拖动事件。源码:https://github.com/limingios/wxProgram.git 中的No.6
小程序的事件触发
通过行为进行的人机交互方式
类似于html的onClick,onChange事件等等
- 官方的阐述
https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html


- 程序演示点击
//events.js
//获取应用实例
const app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
clickMe: function(){
console.log("你点击我这里出来了!")
}
})
<!events.wxml-->
<view class="container">
<text bindtap='clickMe'>点我点我我给console显示</text>
</view>


- 数据传递
通过view标签中的data来绑定数据
<!events.wxml-->
<view class="container">
<text data-forName='公众号:编程坑太多' data-forUser='开发人员' data-forDate='201800805' bindtap='clickMe'>点我点我我给console显示</text>
</view>
//events.js
//获取应用实例
const app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
clickMe: function(e){
console.log("你点击我这里出来了!")
console.log(e)
console.log(e.currentTarget.dataset.fordate)
}
})
- 仔细观察的老铁会发现一个问题
在wxml里面forData是大写,在js里面的fordata自动变成了小写,这就是微信自己帮咱们把驼峰形式做了转换。



PS:小程序的事件基本就是这样,事件使用,事件分类,事件详情,这三个方向来使用。