问题:
uniapp H5端在微信webview内双标题
目标/呈现结果:
单一标题显示,显示微信webview标题,并且顶部导航栏存在,包含返回按钮
解决方案:
// template.h5.html
<head>
...
<script>
window.onload = function() {
const isWechat = /MicroMessenger/i.test(window.navigator.userAgent);
if (!isWechat) {
const wrapper = document.querySelector('.user-agent-wrapper');
wrapper.classList.remove('user-agent-wrapper');
}
}
</script>
</head>
<body>
...
<div class="user-agent-wrapper">
<div id="app"></div>
</div>
</body>
// App.vue
<style>
.user-agent-wrapper {
.uni-page-head__title {
display: none;
}
}
</style>
解决方案原理:
- display 隐藏标题
- 判断宿主环境,是否作用css(间接作用)
- 对#app进行包装,外层div的class决定作用css(直接作用)