VUE单页面应用在进行百度统计时,只能统计到主页的访问次数,而不能统计到子页面。
这是因为,如果按照以上百度统计提供的方法添加统计代码,在VUE单页面应用,如果不刷新,只会加载一次 index.html, 加载后会继续加载main.js、App.vue、index.js等,所以不论在页面内怎么切换,都只会统计到一次。
解决方法是,使用router的beforeEach 全局前置守卫或 方法在每次路由之前调用百度统计代码。
<script>
var _hmt=_hmt||[];
(function() {
var hm=document.createElement("script");
hm.src="https://hm.baidu.com/hm.js?a5ba0f0abe46b3c4d0539c55e4d02098";
var s=document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm,s);
})();
</script>
router.beforeEach((to, from, next) => {
if (to.path) {
window._hmt.push(['_trackPageview', '/#' + to.fullPath]);
}
next()
});
或者
router.afterEach(function(to, from) {
if (to.path) {
window._hmt.push(['_trackPageview', '/#' + to.fullPath]);
}
});
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有