事件绑定@

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

  1. <button @click="tapHandle">点击</button>

事件函数定义在methods中

  1. methods:{
  2. tapHandle(){
  3. console.log('点击完成')
  4. }
  5. }

事件传参

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

  1. //template
  2. <button @click="tapHandle">点击这里</button>
  3. //script
  4. methods:{
  5. tapHandle (e) {
  6. console.log(e)
  7. }
  8. }

如果给事件函数传递参数了,则对应的事件函数形参接收的则是传递过来的数据


  1. <template>
  2. <view class="content">
  3. <view class="box2" hover-class="box2-active" >
  4. <view class="box" hover-class="box-active" hover-stop-propagation>
  5. this is a box
  6. </view>
  7. </view>
  8. <view class="box" hover-class="box-active">
  9. this is a box
  10. </view>
  11. <view class="">
  12. this is a box1
  13. </view>
  14. <button type="default">按钮</button>
  15. <button type="primary" disabled="">按钮</button>
  16. <button type="warn" loading=""></button>
  17. <image src="https://img0.baidu.com/it/u=2636535523,1482743544&fm=26&fmt=auto" mode="aspectFill"></image>
  18. <image src="https://img0.baidu.com/it/u=2636535523,1482743544&fm=26&fmt=auto" mode="aspectFit"></image>
  19. <view v-for="(item,index) in arr" :key='item.id'>
  20. 序号:{{item.id}},名字:{{item.name}},年龄:{{item.age}}
  21. </view>
  22. <button type="primary" v-on:click="clickHandle(20,$event)">按钮</button>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. title: 'Hello',
  30. arr:[
  31. {
  32. name:'name1',
  33. age:'18',
  34. id:1
  35. },
  36. {
  37. name:'name2',
  38. age:'22',
  39. id:2
  40. },
  41. {
  42. name:'name3',
  43. age:'17',
  44. id:3
  45. }
  46. ]
  47. }
  48. },
  49. onLoad() {
  50. },
  51. methods: {
  52. clickHandle(e){
  53. console.log("点击完成",num,e)
  54. }
  55. }
  56. }
  57. </script>
  58. <style>
  59. .box{
  60. height: 100rpx;
  61. width: 100rpx;
  62. background: #0000FF;
  63. }
  64. .box-active{
  65. background:#ff5500;
  66. }
  67. .box2{
  68. height: 200rpx;
  69. width: 200rpx;
  70. background-color: red;
  71. }
  72. .box2-active{
  73. background-color: blue;
  74. }
  75. </style>