为了保证不重复调用广告组件,我们在全局app.vue文件引入,赋值到公共实例上 app.vue文件
<script>
import * as config from '@/utils/config.js'
export default {
onLaunch: function() {
this.craeteVideo()
},
methods: {
craeteVideo() {
let _this = this;
let rewardedVideoAd = tt.createRewardedVideoAd({
adUnitId: '流量主id'
})
rewardedVideoAd.onLoad(() => {
console.log('拉取激励广告成功')
})
rewardedVideoAd.onError((err) => {
console.log('拉取激励广告失败')
})
config.videoAd = rewardedVideoAd
rewardedVideoAd.onClose((res) => {
if (res && res.isEnded) {
console.log('激励广告全部加载完成')
} else {
console.log('激励广告中途强制关闭')
}
})
},
}
};
</script>
config.js文件
export const videoAd = null
页面使用
<template>
<view class="jinrituijian" @click="click()">
</template>
<script>
import * as config from '@/utils/config.js'
export default {
methods: {
click() {
if (config.videoAd) {
config.videoAd.show(() => {
})
}
}
}
}
</script>
|