立刻有
全部
技术
PHP
MySQL
前端
Linux
JAVA
退出
编辑文章
选择分类
PHP
MySQL
前端
Linux
Java
工具
选择专栏
设计模式
java基础
Angular学习
Java面试题
描述:
Google广告在Angular8网站部署
封面图上传 :
+
点击上传图片
### 在Angular8开发的网站部署Google广告 Google广告代码是包含script代码的,如果直接复制Google广告到Angular的html页面上,编译时是会去除掉script标签代码,具体可看这个GitHub文章:[传送门](https://github.com/angular/angular/issues/4903) ### 新建component - component的template写上google广告的代码,不包含script标签的代码,如下: ```js template: `
` ``` - init方法初始化window.adsbygoogle ```js ngAfterViewInit() { try{ (window['adsbygoogle'] = window['adsbygoogle'] || []).push({}); }catch(e){ console.error("error"); } ``` - 完整代码 ```js import { Component, AfterViewInit} from '@angular/core'; // @Component({ selector: 'app-commemoration-ad', template: `
` }) export class CommemorationAdComponent implements AfterViewInit { constructor() { } ngAfterViewInit() { try{ (window['adsbygoogle'] = window['adsbygoogle'] || []).push({}); }catch(e){ console.error("error"); } } } ``` #### html引入模块 ```
``` #### index.html引入js文件 ```html ``` ### 注意 如果是定义的公共模块,需要在模块里面申明
保存文章