- 引言:公司为统计用户的点击率,提高产品的转化率,引入一段谷歌分析代码,要求在该按钮和链接所在的页面激活这段代码,放在< head >标签中
直接使用nuxt中的head方法
<script>
export default {
data() {
return {
title: 'Hello World!'
}
},
head() {
return {
title: this.title,
meta: [
{
hid: 'description',
name: 'description',
content: 'My custom description'
}
]
}
}
}
</script>
- 发现并没有注入js的说明,但是原理都是一样的,head标签下的标签,有什么属性,只是换了中表达方式,有两种注入方式:
head() {
return {
title: this.title,
meta: [
{
hid: 'description',
name: 'description',
content: 'My custom description'
}
],
script:[
{
type: 'text/javascript',
src:' https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js',
body: true
}
]
}
script:[
{
type: 'text/javascript',
src:' https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js',
innerHTML:`function(a,h,c,b,f,g){......};`
}
]
- 之后就可以在当前页面调用这个function了从而激活分析代码,显示在head标签下:
<head>
....
<script type="text/javascript">
function(a,h,c,b,f,g){......};
</script>
....
</head>