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

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

###### 应该是这样简单的
```
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](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2f85e6eab3bf46e693219cf5b379c605~tplv-k3u1fbpfcp-jj-mark:0:0:0:0:q75.image#?w=1011&h=214&s=34951&e=png&b=332b00)

然后开始各种网上找答案

![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7015765dad21496e86db26b3b6428456~tplv-k3u1fbpfcp-jj-mark:0:0:0:0:q75.image#?w=2908&h=61&s=26884&e=png&b=d2e2fc)

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

以上都没有解决我的问题
###### 回到最原始的时候,微信要求该方法必须是用户调用,于是机灵了一下,不用bindtap直接用click。得到了我想要的结果。
`<button @click="openSettings()">打开</button>`

![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8883e121151145749748875d173a3ced~tplv-k3u1fbpfcp-jj-mark:0:0:0:0:q75.image#?w=492&h=394&s=17500&e=png&b=fdfdfd)

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

posted @ 2024-01-25 18:35  FannieGirl  阅读(194)  评论(0编辑  收藏  举报