版權聲明:本文為CSDN博主「鍾文輝」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_39753974/article/details/80322643
在單頁面中,要是只加在head中的話那是沒用的,因為只能運行一次。
在百度中復制代碼
將代碼放在index.html的head中
<script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?你的代碼"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script>
在路由的beforeEach函數中使用
// router.js let router= new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ { path: '*', name: 'error_404', component: error_404 } ] }); router.beforeEach((to, from, next) => { if (to.path) { _hmt.push(['_trackPageview', '/#' + to.fullPath]); } next(); }
檢測是否安裝成功
打開控制台查看網絡請求
每次路由變化就會發出一個叫hm.gif的請求
兩個小時候再進網頁就會有統計數據