首页 > 编程语言 >如何解决小程序打开授权报错openSetting:fail can only be invoked by user TAP gesture

如何解决小程序打开授权报错openSetting:fail can only be invoked by user TAP gesture

时间:2023-10-11 17:36:18浏览次数:58  
标签:function invoked TAP res console 报错 openSetting 授权 wx

要解决这个问题,你需要在页面上为 openSetting 接口添加一个点击事件。根据微信小程序的文档,openSetting 接口只能通过用户点击行为(如 tap 事件)来触发。以下是一个简单的示例代码:

<!-- index.wxml -->  
<view>  
  <button bindtap="openSetting">打开授权设置</button>  
</view>  
// index.js  
Page({  
  openSetting: function () {  
    wx.openSetting({  
      success: function (res) {  
        console.log('授权设置成功', res);  
      },  
      fail: function (err) {  
        console.log('授权设置失败', err);  
      }  
    });  
  }  
}); 

在这个示例中,当用户点击按钮时,会触发 openSetting 方法,从而打开授权设置界面。这种方式符合微信小程序的交互规范,可以避免报错。

由于wx.showModal方法里也能合理调用wx.openSetting方法,但也报了同样的错误,原因也是没有使用正确的交互方式导致的

要解决这个问题,你需要在页面上为这两个接口添加一个点击事件。例如,你可以在页面上添加一个按钮,当用户点击该按钮时,调用 wx.showModal 接口,并在模态框中显示一个授权设置按钮,当用户点击授权设置按钮时,再调用 wx.openSetting 接口。以下是一个简单的示例代码:

<!-- index.wxml -->  
<view>  
  <button bindtap="showModal">打开授权设置</button>  
</view>  
// index.js  
Page({  
  showModal: function () {  
    wx.showModal({  
      title: '授权设置',  
      content: '请授权',  
      success: function (res) {  
        if (res.confirm) {  
          wx.openSetting({  
            success: function (res) {  
              console.log('授权成功', res);  
            },  
            fail: function (err) {  
              console.log('授权失败', err);  
            }  
          });  
        } else {  
          console.log('授权失败');  
        }  
      },  
      fail: function (err) {  
        console.log('授权失败', err);  
      }  
    });  
  }  
}); 

 在这个示例中,当用户点击按钮时,会触发 showModal 方法,从而打开授权设置界面。当用户点击授权设置按钮时,会触发 wx.openSetting 接口。这种方式符合微信小程序的交互规范,可以避免报错

当然以上的交互规范到后面可能腾讯官方又会作出调整,仅做参考,极具时效性。

 

标签:function,invoked,TAP,res,console,报错,openSetting,授权,wx
From: https://www.cnblogs.com/murenziwei/p/17757733.html

相关文章

  • MySQL连接数超出最大值报错及修改
    报错:createconnectionSQLException,url:jdbc:mysql://xx.xxx.x.xx:6306/hksk-cloud-mes?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai,errorCode1040,s......
  • 【读论文】CM-Gen: A Neural Framework for Chinese Metaphor Generation with Explic
    为了更好的阅读体验,请点击这里由于发不出论文,所以找点冷门方向做一做。从汉语比喻开始。读完这篇论文之后我觉得COLING这方向我上我也行(ε=ε=ε=┏(゜ロ゜;)┛题目:CM-Gen:ANeuralFrameworkforChineseMetaphorGenerationwithExplicitContextModelling论文链接代......
  • @Test下jedisManager.getJedis报错NullPointerException
    原文链接:https://www.longkui.site/error/test%e4%b8%8bjedismanager-getjedis%e6%8a%a5%e9%94%99nullpointerexception/4813/报错代码:importorg.junit.Test;importredis.clients.jedis.Jedis;@AutowiredprivateJedisManagerjedisManager;publicclassTestDem......
  • c++编译报错解决办法
    所有的警告都被当作是错误在全局域:cc1plus:错误:unrecognizedcommandlineoption‘-Wno-unknown-warning’[-Werror]cc1plus:所有的警告都被当作是错误解决办法:去除CMakeLists.txt中的-Werror......
  • 问题记录贴:vue-i18n在弹出框中$t()报错:TypeError: Cannot read property '_t' of unde
    网上有用的解决方法:vue国际化在弹出框中$t()报错:TypeError:Cannotreadproperty'_t'ofundefined大佬给出的解决方法:弹窗是一个新的Vue对象,只需要单独给弹窗重新绑定i18n即可。代码://dialog/main.jsimportcustomDialogfrom'./dialog.vue'importi18nfrom'@/i18n'......
  • python报错cannot import name ‘compare_ssim‘ from ‘skimage.measure‘
    问题原因: scikit-image版本过高导致 解决办法: 重装,安装低版本 pipuninstallscikit-imagepipinstallscikit-image==0.15.0-U-ihttps://pypi.tuna.tsinghua.edu.cn/simple ......
  • vue3+vite import 引入ThreeBSP库 报错
    我在网上查了一下先用npm下载了三方包npmithree-js-csg再使用constThreeBSP=require('three-js-csg')(THREE)的方法引入出现了这个报错查了是因为require是webpack里的vite不支持所以找不到然后我就尝试使用import的方法引入importThreeBSPfrom'three-js......
  • fastapi swagger文档无法渲染问题
    由于fastapi默认使用国外的cdn获得支持swagger文档的js和css文件,所以在国内,/docs接口渲染不出来.解决方法1#在app生效之前,将生成swagger文档的方法替换掉.(在fastapi为:fastapi.openapi.docs.get_swagger_ui_html)​fromfastapiimportFastAPI,applicationsfromfa......
  • 报错Intel MKL FATAL ERROR: Cannot load libmkl_core.so.的一种解决方法
    问题今天上80服务器跑mdistiller的代码时,意外发现torch、numpy都不能用了T_T以torch为例,出现如下报错情况以numpy为例,出现如下报错情况我们先看看报错信息,这个报错来自InterMKL。InterMKL全称是TheIntelMathKernelLibrary,它是一个主要是用于科学计算的共享库,提供了很......
  • 执行wordcount报错及解决
    今天在执行wordcount词频统计时报错执行语句为hadoopjarshare/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jarwordcountwcinputwcoutput报错如下 这表示指定的输入路径hdfs://hadoop102:8020/user/atguigu/wcinput不存在然后我打开hadoop可视化网页一看确实......