在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?xxxx";
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?xxxx";
hm.id = "baidu_tj";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
})
}
id換成自己的,然后在nuxt.config,js中引入文件