首页 > 解决方案 > Google Adsense 广告在 Angular 的所有组件上实施时不显示广告?

问题描述

我在我的网站上使用 Google Adsense,广告显示成功,但是当我想在一个组件中显示广告并在另一个组件中显示广告时,它只在一个组件中工作,并且每次它在一个组件上工作(即在左侧)

组件一

HTML

<div class="text-center">
  <ins class="adsbygoogle ads_resp" style="display:inline-block" data-ad-client="publisherid" data-ad-slot="ad unit id"
       data-full-width-responsive="true"></ins>
</div>

TS

声明界面 Window { adsbygoogle: any[]; }

声明 var adsbygoogle: any[];

export class OneComponent implements OnInit {
 ngAfterViewInit() {
    setTimeout(() => {
     try {
        (adsbygoogle = window["adsbygoogle"] || []).push({});
         /*           or

    (window["adsbygoogle"] = window["adsbygoogle"] || []).push({}); Both are wrking finr*/
      } catch (e) {
        console.error('adsid',e);
      }
    }, 300);
  }
}

相同的代码写在另一个组件中

并且这两个组件都包裹在另一个组件中,例如

我想将广告放在一个组件和另一个组件中,当我将广告放在两个组件中时,左侧的广告显示,右侧只有空白区域,如果我只在一个或其他组件中使用广告,那么它可以正常工作.

一件更有趣的事情,在网站的主页(左侧)上,当我放两个广告代码时,两个广告会显示,但是当同样的放在右侧时,它就不起作用了

在其他页面中,只有 1 个广告正在显示,无法显示两个广告,这两个广告仅显示在主页的左侧。

标签: javascriptangulartypescriptadsadsense

解决方案


推荐阅读