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

The*_*eal 9 adsense angular

我有什么方法可以在Angular 2应用上使用Google Adsense广告吗?

在Angular 2组件中看到了这个Google AdSense广告?但答案实际上并不适合我.我看到的是空格而不是广告.我想要放置的是响应式广告.我完全按照上面问题的答案说,用这个模板代码:

<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>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

小智 8

链接中的答案对我有用。更好的是,我设法改进了可与动态广告位信息一起使用的代码。

banner.component.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);
} }
Run Code Online (Sandbox Code Playgroud)

将其添加到您希望广告展示的html中

<google-adsense [data]="your_slot_id"></google-adsense>
Run Code Online (Sandbox Code Playgroud)

在主HTML文件中添加Google Adsense脚本

<script async src=//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js>/script>
Run Code Online (Sandbox Code Playgroud)

不要忘记添加app.module。

另一方面,Google adsense标语不会在localserver中显示,如果在服务器中,则需要花费一些时间才能显示出来。您可以将备用广告属性更改为

Fill space with a solid color 
Run Code Online (Sandbox Code Playgroud)

在Google Adsense门户中。