解决建议:
1、在success()函数作用域前,声明let self = this;将self替换后续的this
2、使用箭头函数来确保 this 的上下文不变。(这个我没使用)
1、问题阐述:
在完成加入组织的逻辑时,使用了showActionSheet(),在进行操作处理时,success()内,this.addorgData无法识别出来。
2、问题产生原因:
this的指向发生变化:
this 关键字在 success 回调函数中的上下文不再指向 Vue 实例或组件本身,在JavaScript 中:函数作用域会改变 this 的指向,因此在 success 回调函数中,this 指向的是 success 函数本身,而不是 Vue 实例或组件。所以此处使用了self。
3、解决思路:
在success()函数作用域前,声明let self = this;将self替换后续的this
代码举例:
let self = this;
//在success外声明
success(res) {
if (parseInt(res.tapIndex) === 0) {}
else {
console.log('点击了申请加入组织', orgName);
addMembers(self.addOrgData).then(res => {})
//替换为self.你的属性。
uni.showToast({
title:'申请成功',
})
}
},