uniapp嵌套webview页面时,出现白屏解决方案——subnvue加载弹框
1.封装subnvue公共组件:loading.nvue文件,注意是nvue文件
<template>
<view class="content"></view>
</template>
<script>
export default {
data() {
return {
}
},
onReady() {
uni.showLoading({
title: '加载中...',
mask: true
});
}
}
</script>
2.在pages.json文件中配置subnvue:
{
"path": "首页路径",
"style": {
"navigationBarTitleText": "首页",
"navigationBarBackgroundColor":"#1677FF",
"navigationBarTextStyle":"white",
"app-plus": {
"subNVues":[{
"id": "loadingSubnvue", // 唯一标识
"path": "pages/monitor/history/subNVue/loading", // 页面路径
"type": "popup",//原生子窗口内置样式,可取值:'popup',弹出层;"navigationBar",导航栏
"style": {
"position": "dock",
"dock": "bottom",
"background": "transparent"
}
}]
}
}
},
3.在页面中使用loading.nvue文件:
<web-view :src="webviewUrl" @message="handleMessage" :update-title="false"></web-view>
onLoad(){
this.subnvue_open();
},
onUnload() {
uni.$off('loadingSubnvue');//移除监听配置的原生子窗体
},
methods:{
//打开配置的原生子窗体
// #ifdef APP-PLUS
subnvue_open() {
this.subNVue = uni.getSubNVueById('loadingSubnvue'); //通过id获取nvue子窗体
this.subNVue.show();
},
// #endif
// 接收webvue的Html发送过来的消息 (APP端) 手机真机测试看打印结果
handleMessage(evt) {
//如果html页面加载完成,则关闭loading弹窗
if(evt.detail.data[0].show == false){
uni.hideLoading();
}
}
}
4.webview页面中监测html页面是否加载完成:
//加载发生变化时调用
document.onreadystatechange = subSomething
function subSomething() {
// 判断是否加载完成
if (document.readyState == 'complete') {
document.getElementById('app').style.display = 'block'
console.log('加载完成','-------------------')
// 加载完成后传值给uniapp
document.addEventListener('UniAppJSBridgeReady', function() {
uni.getEnv(function(res) {
console.log('当前环境:' + JSON.stringify(res));
});
uni.postMessage({
data: {
show:false
},
})
})
}
}