# uni中的事件

# 事件绑定

在uni中事件绑定和vue中是一样的,通过v-on进行事件的绑定,也可以简写为@

<button @click="tapHandle">点我啊</button>
1

事件函数定义在methods中

methods: {
  tapHandle () {
    console.log('真的点我了')
  }
}
1
2
3
4
5

# 事件传参

  • 默认如果没有传递参数,事件函数第一个形参为事件对象

    // template
    <button @click="tapHandle">点我啊</button>
    // script
    methods: {
      tapHandle (e) {
        console.log(e)
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
  • 如果给事件函数传递参数了,则对应的事件函数形参接收的则是传递过来的数据

    // template
    <button @click="tapHandle(1)">点我啊</button>
    // script
    methods: {
      tapHandle (num) {
        console.log(num)
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
  • 如果获取事件对象也想传递参数

    // template
    <button @click="tapHandle(1,$event)">点我啊</button>
    // script
    methods: {
      tapHandle (num,e) {
        console.log(num,e)
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
上次更新: 2021/2/22 上午10:53:04