如何在 Vue.js 组件中添加 google adsense?

Анд*_*рей 8 javascript adsense vue.js

我已经尝试将其作为自定义指令,但 JS 拒绝任何字符串中的脚本标记(未终止的文字字符串)。此外,vue-google-adsense 和 vue-adsense 插件对我不起作用,因为它们没有获得 Adsense 提供的所有参数,因此广告变得没有响应等。

Jef*_*rod 10

index.html文件中,从 #app 中添加 adsense 代码:

<div id="app"></div>
<div id="divadsensedisplaynone" style="display:none;">
    <!-- put here all adsense code -->
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
        style="display:block"
        data-ad-client="ca-pub-xxxxxx"
        data-ad-slot="xxxxxx"
        data-ad-format="auto"
        data-full-width-responsive="true"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
</div>
Run Code Online (Sandbox Code Playgroud)

在您的主App.vue文件或任何 Vue 文件中,在您希望显示广告的位置添加以下内容(您可以自由更改样式):

<div id="adsgoeshere" style="background: #1d1f29; padding-top:60px; text-align: center;" v-html="adsenseContent"></div>
Run Code Online (Sandbox Code Playgroud)

data添加:

 adsenseContent: ''
Run Code Online (Sandbox Code Playgroud)

最后,在mounted函数中,添加:

this.adsenseContent = document.getElementById('divadsensedisplaynone').innerHTML
Run Code Online (Sandbox Code Playgroud)

就是这样!你不需要任何图书馆。