事件
事件绑定
在 uni 中事件绑定和 vue 中是一样的,通过 v-on 进行的事件绑定,也可以简写为@
html
<button @click="tapHandle">点我啊</button>
事件定义在 methods 中
js
methods: {
tapHandle() {
console.log('tapHandle')
}
}
事件传参
- 默认如果没有传递参数,事件函数第一个形参为事件对象
js
// template
<button @click="tapHandle">点我啊</button>
// script
methods: {
tapHandle (e) {
console.log(e)
}
}
- 如果给事件函数传递参数了,则对应的事件函数形参接收的则是传递过来的数据
js
// template
<button @click="tapHandle(1)">点我啊</button>
// script
methods: {
tapHandle (num) {
console.log(num)
}
}
- 如果获取事件对象也想传递参数
html
// template
<button @click="tapHandle(1,$event)">点我啊</button>
// script methods: { tapHandle (num,e) { console.log(num,e) } }