Using Google Adsense ads on Angular 2 application

后端 未结 2 1268
广开言路
广开言路 2021-01-31 22:09

Is there any way to use Google Adsense ads on an Angular 2 app?

I have seen this Google AdSense ads in Angular 2 components? but the answer isn\'t actually working for m

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 22:40

    The answer in this link is worked for me. Even better I managed improve that code which can be use with dynamic Slot info.

    banner.component.ts

    import { Component, AfterViewInit, Input } from "@angular/core"
    @Component({
    moduleId: module.id,
    selector: "google-adsense",
    template: ` 

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

    Add this in your html where you want your ad to appear

    
    

    Add google adsense script in your main html file

    
    
                                     
                  
提交回复
热议问题