this 一直指向前一个对象
2020-03-12
1 min read
在使用了react-native-admob插件之后,第二次始终是指向前面的一个this对象。 调试了蛮久,发现是因为我每次调用广告的时候都会addListener,然后插件其实会存起来。
首先我们应该调用
removeAllListeners();
if (global.loadingReward) {
console.log('正在加载广告,跳过!!!');
return;
}
this.hasRecv = false;
console.log('开始加载广告!!!');
global.loadingReward = true;
AdMobRewarded.setTestDevices([AdMobRewarded.simulatorId]);
//AdMobRewarded.setAdUnitID('ca-app-pub-9280092940596395/3561826862');
AdMobRewarded.setAdUnitID('ca-app-pub-3940256099942544/5224354917');
AdMobRewarded.removeAllListeners();
AdMobRewarded.addEventListener('rewarded', reward => {
if (this.hasRecv){
return;
}
this.hasRecv = true;
console.log('AdMobRewarded => rewarded', reward);
global.loadingReward = false;
successCallback();
});
AdMobRewarded.addEventListener('adFailedToLoad', error => {
global.loadingReward = false;
console.log('failed:', error);
errorCallback();
});
AdMobRewarded.addEventListener('adClosed', ()=>{
console.log('adClosed!!!');
if(global.loadingReward == false){
return;
}
global.loadingReward = false;
errorCallback();
})
AdMobRewarded.requestAd()
.then(() => AdMobRewarded.showAd())
.catch(error => {
global.loadingReward = false;
console.log('failed:', error);
errorCallback();
});