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

相关推荐
小码哥06819 分钟前
短剧平台源码定制交付:批量导入剧集与媒体资源校验流程
前端·vue.js·媒体·短剧小程序·短剧系统·抖音短剧
晓得迷路了23 分钟前
栗子前端技术周刊第 155 期 - 2026 HTML 现状调查报告、Solid 2.0...
前端·javascript·webpack
艾莉丝努力练剑43 分钟前
【AI接入大模型SDK】Markdown格式解析
前端·c++·人工智能·学习·ai·架构
LinXunFeng1 小时前
A2UI 1.0 详解:让 AI 生成 Flutter 界面,还不怕它乱写
前端·flutter·agent
FRing0315 小时前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
Bczheng115 小时前
组件化服务端渲染模式SSR
前端
whitelbwwww16 小时前
CUDA C++实战--中级
开发语言·前端·c++
张紫娃16 小时前
Chrome插件-Stylus(屏蔽博客广告)
前端·chrome·stylus
Yannik16 小时前
在 Apple Silicon Mac 上用 Jenkins + Fastlane 搭建 iOS CI:从自动打包到 MR 检查流水线的完整实践
前端
Nayana16 小时前
《Web 到 HarmonyOS》-- 打包:Vite 产物 → HAP / HAR / HSP
前端