vue 使用Adsense错误:TagError: adsbygoogle.push() error: All ins elements in the DOM with class=adsbygoogle already have ads in them.

发布时间 2023-08-26 05:37:05作者: zjhgx
              <div class="adsenseunit">
                <!-- in-article-aquare2 -->
                <ins
                  class="adsbygoogle"
                  style="display: block"
                  data-ad-client="ca-pub-3935005489954231"
                  data-ad-slot="6650477400"
                  data-ad-format="auto"
                  data-full-width-responsive="true"
                ></ins>
              </div>


            <div class="adsenseunit">
              <!-- in-article-aqure -->
              <ins
                class="adsbygoogle"
                style="display: block"
                data-ad-client="ca-pub-3935005489954231"
                data-ad-slot="4387276146"
                data-ad-format="auto"
                data-full-width-responsive="true"
              ></ins>
            </div>
 mounted() {
    //启动谷歌unit广告
    let adsenseUnitLength = document.getElementsByClassName('adsbygoogle');
    window.onload = function () {
      for (let i = 0; i < adsenseUnitLength.length; i++) {
        (adsbygoogle = window.adsbygoogle || []).push({});
      }
    };

}

在页面中有两个adsense unit,但 document.getElementsByClassName('adsbygoogle') 这个得到的结果是3,可能是因为自动广告开启的缘故,会自动在页面中插入一个带有class=“adsbygoogle” <ins>,导致重复填充。不能用'adsbygoogle这个class name。