发布于 5年前

Nuxt.js 如何引入百度统计

// plugins/tongji.js
if (process.BROWSER_BUILD && process.env.NODE_ENV === 'production') {
  var _hmt = _hmt || []
  ;(function() {
    var hm = document.createElement('script')
    // hm.src = 'https://hm.baidu.com/hm.js?[你的唯一标识,此处以片段为例]'
    // hm.src = 'https://hm.baidu.com/hm.js?61fb58293814d42803e9ef530137a8c0'
    hm.id = 'baidu_tj'
    var s = document.getElementsByTagName('script')[0]
    s.parentNode.insertBefore(hm, s)
  })()
}

export default ({ app: { router }, store }) => {
  router.afterEach((to, from) => {
    var _hmt = _hmt || []
    ;(function() {
      document.getElementById('baidu_tj') && document.getElementById('baidu_tj').remove()
      var hm = document.createElement('script')
    //   hm.src = 'https://hm.baidu.com/hm.js?[你的唯一标识,此处以片段为例]'
    // hm.src = 'https://hm.baidu.com/hm.js?61fb58293814d42803e9ef530137a8c0'
      hm.id = 'baidu_tj'
      var s = document.getElementsByTagName('script')[0]
      s.parentNode.insertBefore(hm, s)
    })()
  })
}

// nuxt.config.js
module.exports = {
    //...

    plugins: [
        { src: '@/plugins/tongji.js', ssr: false }
    ],
}
©2020 edoou.com   京ICP备16001874号-3