Google AdSense ads in Angular 2 components?

前端 未结 4 1173
囚心锁ツ
囚心锁ツ 2020-12-24 09:18

I\'m trying to load some Responsive ads in an AdComponent in my app. The component is dead simple:

import { Component } from \'@angular/core\';
         


        
4条回答
  •  孤独总比滥情好
    2020-12-24 09:44

    This is working for me:

    TopBannerComponent.ts ==>

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

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

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

    
    

    Add google adsense script in your main html file

     
    

提交回复
热议问题