原文地址:http://www.bowen-tech.top/art...在Angular开发的网站上部署谷歌广告。脚本标签代码将被删除。具体可以看这篇GitHub文章:传送门用google广告代码创建一个新的component组件模板,不包括script标签代码,如下:template:`
`init方法初始化window.adsbygooglengAfterViewInit(){try{(window['adsbygoogle']=window['adsbygoogle']||[]).push({});}catch(e){console.error("error");}完整代码import{Component,AfterViewInit}from'@angular/core';//@Component({selector:'app-commemoration-ad',template:`
`})导出类CommemorationAdComponent实现AfterViewInit{constructor(){}ngAfterViewInit(){try{(window['adsbygoogle']=window['adsbygoogle']||[]).push({});}catch(e){console.error("error");}}}htmlimportmodule
index.html导入js文件中添加此内容注意,如果是定义的公共模块,需要在模块中声明