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
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