uniapp打包H5⾃定义模板
需要使⽤⾃定义模板的场景,通常有以下⼏种情况:
调整页⾯ head 中的 meta 配置
补充 SEO 相关的⼀些配置(仅⾸页)
加⼊百度统计等三⽅js
使⽤⾃定义模板时:
1. 在项⽬根⽬录下创建⼀个template.h5.html⽂件
2. 复制下⾯的基本模板内容,到这个html⽂件,在此基础上修改meta和引⼊js
3. 在 manifest.json->h5配置->模板⽴即template 节点中关联这个html⽂件的路径
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"content="IE=edge">
<script>
var coverSupport ='CSS'in window &&typeof CSS.supports ==='function'&&(CSS.supports('top: env(a)')||CSS.supports('top: constant(a)'))
document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0 '+(coverSupport ?', viewport-fit=cover':'')+'" />')
</script>
<link rel="stylesheet"href="<%= BASE_URL %>static/index.<%= VUE_APP_INDEX_CSS_HASH %>.css"/>
</head>
<body>
<noscript>
<strong>Please enable JavaScript to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
如果需要请求接⼝获取meta参数的话,可以引⼊下⾯的⾃定义AJAX
function ajax(url){
return new Promise(function(resolve, reject){
app模板网站
var xhr =new XMLHttpRequest()
xhr.open('GET', url)
if(this.status ===200){
sponse)
}else{
reject(new Error(this.statusText))
}
}
xhr.send()
})
}
ajax('接⼝').then(function(res){
console.log('成功打印:', res)
})
创建并添加meta并更改标题
var hm = ateElement("meta");
hm.name ='keywords'//添加关键字
var s = ElementsByTagName("meta")[0]; s.parentNode.insertBefore(hm, s);
var hmt = ateElement("meta");
hmt.name ='description'//添加描述标签
var st = ElementsByTagName("meta")[1]; st.parentNode.insertBefore(hmt, st);
document.title ='xxx'//更改标题
⾄此H5⾃定义模板已更改完毕
完整代码如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script>
var coverSupport ='CSS'in window &&typeof CSS.supports ==='function'&&(CSS.supports('top: env(a)')||CSS
.supports('top: constant(a)'))
document.write(
'<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'+ (coverSupport ?', viewport-fit=cover':'')+'" />')
function ajax(url){
return new Promise(function(resolve, reject){
var xhr =new XMLHttpRequest()
xhr.open('GET', url)
if(this.status ===200){
sponse)
}else{
reject(new Error(this.statusText))
}
}
xhr.send()
})
}
ajax('接⼝').then(function(res){
console.log('检索:', res)
de ==1){
var data = res.data
var hm = ateElement("meta");
hm.name ='keywords'//添加关键字
var s = ElementsByTagName("meta")[0];
s.parentNode.insertBefore(hm, s);
var hmt = ateElement("meta");
hmt.name ='description'//添加描述标签
var st = ElementsByTagName("meta")[1];
st.parentNode.insertBefore(hmt, st);
document.title ='xxx'//更改标题
}
})
</script>
<link rel="stylesheet" href="<%= BASE_URL %>static/index.<%= VUE_APP_INDEX_CSS_HASH %>.css"/>
</head>
<body>
<noscript>
<strong>Please enable JavaScript to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。