微信小程序事件处理函数参数问题

时间:2019-07-17 09:20:04   收藏:0   阅读:305

初学小程序,不足的地方或者错误的地方请指正,谢谢。

 

背景:在学习小程序开发时,想获取列表渲染中的index值,因为在学习vue时可以给事件处理函数传入参数,想用同样的方法在小程序上应用,发现在catchtap=‘onPostTap(1)‘,传入参数在调用onPostTap时会报Component "pages/posts/posts" does not have a method "onPostTap(1)" to handle event "tap".的错误。

查阅文档发现

 

最终使用自定义属性的方法获取到相应的index,具体代码如下:

wxml部分


<block wx:for="{{postKey}}" wx:key="{{index}}">
  <
view catchtap=‘onPostTap‘ data-index="{{index}}">     <template is="postItem" data="{{...item}}"/>   </view>
</block>

js部分

Page({
  data: {

  },
  onPostTap: function(event) {
  var index = event.currentTarget.dataset.index
    console.log(index) // 打印出相应的index
    //测试其他回调函数的参数问题
    var a = this.fn(6)
    console.log(a) // 6
  },
  fn: function(n) {
    return n
  }
})

结论:小程序中事件处理函数的参数就是event,不能传入其他参数。

原文:https://www.cnblogs.com/Ingots/p/11198520.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!