关于does not have a method xx to handle event "tap"我有话要说

488 阅读1分钟

前言

我正在对接微信小程序订阅消息功能,看了官方文档觉得挺简单的。于是踩坑开始了

应该是这样简单的
wx.requestSubscribeMessage({
  tmplIds: [''],
  success (res) { }
})
你直接调用会得到

订阅失败 {errMsg: "requestSubscribeMessage:fail can only be invoked by user TAP gesture."}

根据这个错误,得到必须要用手动调用(这个要好好理解)。调整后你得到以下代码
 <button open-type="openSetting" bindopensetting="openSettings">打开设置页</button>
 这个按钮会直接打开授权设置页面,目前还不是你的业务范围(你会搜到的大部分答案)
 
 第二种方式是这样绑定bindtap
 <button >授权</button>
 如果你也是用的uniAPP开发
 methods: {
     openSettings(){
         wx.requestSubscribeMessage({
          tmplIds: [''],
          success (res) { }
        })
     } 
 }
第一种方法,目前不需要,你需要的弹出订阅消息授权框
于是方式二,你始终得到Component "pages/index/home" does not have a method "openSettings" to handle event "tap".

image.png

然后开始各种网上找答案

image.png

这里我也把看到的一些答案汇总
  1. bindtap=" openSettings " 是否前后有空格
  2. js文件不需要包一层methods,组件形式需要包一层【这里我挪进去挪出来试,无用】
  3. bindtap="这里的函数不要带参数直接写函数名";如果要传参数使用data-xxx的形式传递
  4. 在js中没有定义方法

以上都没有解决我的问题

回到最原始的时候,微信要求该方法必须是用户调用,于是机灵了一下,不用bindtap直接用click。得到了我想要的结果。

<button @click="openSettings()">打开</button>

image.png

总结

这个功能很难吗?我还是觉得简单的,但是我却花了两个小时以上。原本半个小时不到就可以完成的功能