Vue Google 广告的配置

前置条件:已经在Google AdSense 中 添加网站 并通过审核 同时已创建广告单元。

因 VUE 的 Script 配置问题,所以不能直接拷贝内容。

index.html 配置

添加 Google 广告的脚本。

复制代码
//index.template.html
/* *
在head标签中添加 script
【 **** 】:你的发布商 ID
*/
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4797644559430915"
            crossorigin="anonymous"></script>

//index.template.html
//在body标签中为window添加函数
<body>
 
  <div id="q-app"></div>

<script>
    window['googlAdsense'] = function () {
        let chlid = document.getElementsByClassName('google-adsense-slot')
        for (let index = 0; index < chlid.length; index++) {
            (adsbygoogle = window.adsbygoogle || []).push({});
        }
    }
</script>
</body>

组件中

组件中添加应用。

复制代码
onMounted(() => {
   window.googlAdsense();
});

为广告添加占位符。

复制代码
//将广告单元代码添加到盒子中
<template>
            <div class="col google-adsense-slot">
              <ins
                  class="adsbygoogle"
                  style="display: block"
                  data-ad-client="ca-pub-4797644559430915"
                  data-ad-slot="4417932244"
                  data-ad-format="auto"
                  data-full-width-responsive="true"
              ></ins>
            </div>
</template>

https://www.isharkfly.com/t/vue-google/16438/1

相关推荐
马可奥勒留38 分钟前
声明
javascript
轻口味1 小时前
Vue.js 与 RESTful API 集成之使用 Axios 请求数据
前端·vue.js·restful
~废弃回忆 �༄1 小时前
CSS中相对定位使用详情
前端·css·css中相对定位使用详情
团酱2 小时前
控制台报错:The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.
开发语言·javascript·sass
inxunoffice2 小时前
批量压缩与优化 Excel 文档,减少 Excel 文档大小
前端·excel
程序员SKY2 小时前
Vue 系列之:ref、reactive、toRef、toRefs
vue.js
清羽_ls2 小时前
react useEffect函数清除副作用函数执行时机
前端·javascript·react.js
蜜獾云2 小时前
nginx: [error] invalid PID number ““ in “/usr/local/nginx/logs/nginx.pid“
java·前端·nginx
奶糖 肥晨2 小时前
基于 Vue 和 Element Plus 的时间范围控制与数据展示
前端·vue.js·elementui
papapa键盘侠2 小时前
完善机器人:让 DeepSeek 使用Vue Element UI快速搭建 AI 交互页面
vue.js·ui·机器人