1***@qq.com
1***@qq.com
  • 发布:2021-12-17 16:51
  • 更新:2022-02-15 10:04
  • 阅读:423

页面监听event事件无法调用methods的方法,求解

分类:uni-app
关联插件: iData条码扫描插件

var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule")
var modal = uni.requireNativePlugin('modal');
var globalEvent = uni.requireNativePlugin('globalEvent');

export default {
onLoad() {
globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
modal.toast({
message: "收到条码:" + JSON.stringify(e),
duration: 1.5
});
});
this.code(e)
},

methods: {  
    code(e) {  
        console.log(e);  
    }  
}  

}

页面监听event事件无法调用methods的方法,求解

2021-12-17 16:51 负责人:无 分享
已邀请:
y***@loongair.cn

y***@loongair.cn

用that替换this
onLoad() {
let that = this
globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
modal.toast({
message: "收到条码:" + JSON.stringify(e),
duration: 1.5
});
});
that.code(e)
},

falost

falost

把 function(e) 改成箭头函数 this 才生效

该问题目前已经被锁定, 无法添加新回复