uniapp 使用uni.scanCode 扫描成功回调时,出现 *** is not a function

使用uniapp框架,开发app时,使用uni.scanCode 组件,调用手机摄像头进行扫码,扫描成果后,回调中,执行其他函数方法,如:
官方示例:
地址:https://zh.uniapp.dcloud.io/api/system/barcode.html
uni.scanCode({
success: function (res) {
console.log('条码res:' + res);
console.log('条码类型:' + res.scanType);
console.log('条码内容:' + res.result);
this.open(res);
}
});以上根据官网示例代码,扫码成果后,执行一个 open方法,但是,此时,console.log会成果输出,但执行 this.open时,会提示 this.open is not a function
查看整体代码,没有错误,那应该是因为 function的问题,此时,需要将 success: function 修改为箭头函数, 箭头函数可以捕获 this
因此,修改为:
uni.scanCode({
success: (res) =>{
console.log('条码res:' + res);
console.log('条码类型:' + res.scanType);
console.log('条码内容:' + res.result);
this.open(res);
}
});就可以调用了。

该日志由 bemender 于 2024年09月13日 发表
转载请注明文本地址:http://www.bemhome.com/post/203.html