在 Angular 2 应用程序上使用 Google Adsense 广告

2023-12-27

有没有办法在 Angular 2 应用程序上使用 Google Adsense 广告?

我见过这个Angular 2 组件中的 Google AdSense 广告? https://stackoverflow.com/questions/37580561/google-adsense-ads-in-angular-2-components但答案实际上对我不起作用。我看到的是空白区域而不是我的广告。我想要放置的是自适应广告。我完全按照上述问题的答案,使用以下模板代码:

<div class="text-center">
    <ins class="adsbygoogle" style="display:inline-block;height:150px"
    data-ad-client="my ca-pub" data-ad-slot="my ad slot"></ins>
</div>

有任何想法吗?


答案在这link https://stackoverflow.com/a/41629280/7334016对我有用。更好的是,我设法改进了可与动态插槽信息一起使用的代码。

横幅.组件.ts

import { Component, AfterViewInit, Input } from "@angular/core"
@Component({
moduleId: module.id,
selector: "google-adsense",
template: ` <div>
        <ins class="adsbygoogle"
           style="display:block;"
            data-ad-client="write_your_pub_"
    [attr.data-ad-slot]="data"
            data-ad-format="auto"></ins>
         </div>   
            <br>            
  `,

})

export class BannerComponent implements AfterViewInit {
@Input() data;
constructor() {}
ngAfterViewInit() {
setTimeout(() => {
 try {
    (window["adsbygoogle"] = window["adsbygoogle"] || []).push({});
  } catch (e) {
    console.error(e);
  }
}, 2000);
} }

将其添加到您希望广告出现的 html 中

<google-adsense [data]="your_slot_id"></google-adsense>

在您的主 html 文件中添加 google adsense 脚本

<script async src=//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js>/script>

不要忘记添加 app.module。

另一方面,Google adsense 横幅不会显示在本地服务器中,如果它在服务器中,则需要一些时间才能显示。您可以通过将备用广告属性更改为来确保它正在工作

Fill space with a solid color 

在 Google Adsense 门户中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Angular 2 应用程序上使用 Google Adsense 广告 的相关文章