原文地址:http://www.bowen-tech.top/art...html
Google廣告代碼是包含script代碼的,若是直接複製Google廣告到Angular的html頁面上,編譯時是會去除掉script標籤代碼,具體可看這個GitHub文章:傳送門git
template: `<div> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxxxxxx" data-ad-slot="xxxxx" data-ad-format="auto" data-full-width-responsive="true"></ins> </div>`
ngAfterViewInit() { try{ (window['adsbygoogle'] = window['adsbygoogle'] || []).push({}); }catch(e){ console.error("error"); }
import { Component, AfterViewInit} from '@angular/core'; // <!-- tools網站記念日計算橫幅廣告 --> @Component({ selector: 'app-commemoration-ad', template: `<div> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxxxxxx" data-ad-slot="xxxxx" data-ad-format="auto" data-full-width-responsive="true"></ins> </div>` }) export class CommemorationAdComponent implements AfterViewInit { constructor() { } ngAfterViewInit() { try{ (window['adsbygoogle'] = window['adsbygoogle'] || []).push({}); }catch(e){ console.error("error"); } } }
<!--在您但願展現廣告的html中添加此內容--> <app-commemoration-ad></app-commemoration-ad>
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
若是是定義的公共模塊,須要在模塊裏面申明github