Nuxt.js引用谷歌广告代码

摘要:最近另一个网站的谷歌联盟申请下来了,记录一下Nuxt.js如何引用谷歌广告的JS代码,当初找了好久。只配置nuxt.config.js文件就可以,下面贴出来。

最近另一个网站的谷歌联盟申请下来了,记录一下Nuxt.js如何引用谷歌广告的JS代码,当初找了好久。

只配置nuxt.config.js文件就可以,下面贴出来。

// 这段是谷歌广告要引入的JS代码,这里装在一个字符串变量里xxxxx就是你的ID
const absbygoogle = '(adsbygoogle=window.adsbygoogle||[]).push({google_ad_client:"ca-pub-xxxxx",enable_page_level_ads:true});';

export default {
  mode: 'universal',

  /*
  ** Headers of the page
  */
  head: {
    meta: [
      {charset: 'UTF-8'},
      {name: 'viewport', content: 'width=device-width, initial-scale=1, shrink-to-fit=no'},
      {httpEquiv: 'Content-Type', content: 'text/html; charset=UTF-8'},
      {httpEquiv: 'content-language', content: 'zh-CN'}
    ],
    script: [
      // 这里引入谷歌广告的JS文件,async要为true
      {src: 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', async: 'true'},
      // 把上面的JS代码变量引用到这里
      {innerHTML: absbygoogle, type: 'text/javascript', charset: 'utf-8'}
    ],
    // 禁止对JS转义,这一句一定要加上
    __dangerouslyDisableSanitizers: ['script']
  },

  /*
  ** Customize the progress-bar color
  */
  loading: '@/components/loading.vue',

  /*
  ** Global CSS
  */
  css: [
    '@/assets/common.css',
    '@/assets/icon/iconfont.css'
  ],

  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
    '@/plugins/axios'
  ],

  /*
  ** Nuxt.js modules
  */
  modules: [
    // Doc: https://bootstrap-vue.js.org/docs/
    'bootstrap-vue/nuxt',
    '@nuxtjs/axios'
  ],

  /*
  ** Build configuration
  */
  build: {
    extractCSS: {allChunks: true},
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
    }
  }
}

其实加三段代码就可以了,我都用中文注释标出来了,就这样。

本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_5514