Vue项目引入百度统计的正确操作步骤,亲测有效!

1、平台获取统计代码

在这里插入图片描述

2、在head和body中分别添加以下代码
head:

  <script>
    var _hmt = _hmt || [];
  </script>

body:

  <script>
    var _hmt = _hmt || [];
    (function () {
    
    
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?476f388f2f9379b40dd120383cf08647";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>

在这里插入图片描述

3、在main.js中添加以下代码

// 加入百度统计
router.beforeEach((to, from, next) => {
    
    
  if (to.path) {
    
    
    if (window._hmt) {
    
    
      window._hmt.push(['_trackPageview', '/#' + to.fullPath])
    }
  }
  next()
})

在这里插入图片描述

重新build构建部署到服务器上并重启nginx即可,回到百度统计页面代码安装成功!

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44912902/article/details/134074004