uni-app cover-view与web-view组件共用时 v-if由true转false未完全隐藏

uni-app cover-view与web-view组件共用时 v-if由true转false未完全隐藏

开发环境 版本号 项目创建方式
uniapp/App
Windows Windows 10 专业版 20H2 HBuilderX
HBuilderX 3.4.8
Android Android 11
华为
nova3

产品分类:uniapp/App

PC开发环境操作系统:Windows

PC开发环境操作系统版本号:Windows 10 专业版 20H2

HBuilderX类型:正式

HBuilderX版本号:3.4.8

手机系统:Android

手机系统版本号:Android 11

手机厂商:华为

手机机型:nova3

页面类型:vue

vue版本:vue2

打包方式:云端

项目创建方式:HBuilderX


示例代码:

<template>
<view class="container">
<!-- 观看倒计时 -->
<cover-view class="countdown" >
<cover-view>您还需观看</cover-view>
<cover-view style="color:#E60000;margin-left:10px;">{{countDownInterval}}{{isVerifyFace}}
</cover-view>
</cover-view>
<!-- pdf文件 -->
<web-view :src="pdfSrc"></web-view>
<!-- 蒙版弹窗 -->
<cover-view class="mask" v-if="isVerifyFace">
<cover-view class="mask-content">
测试
</cover-view>
</cover-view>
</view>
</template> 
<script>
export default {
data() {
return {
isVerifyFace: false, // 是否检测人脸
countDownInterval: 0, // 定时器
pdfSrc: '', // pdf文件地址
}
},
onShow() {},
onLoad(option) {
this.isVerifyFace=true
setTimeout(()=>{this.isVerifyFace=false
},2000)
},
beforeDestroy() {},
methods: {}
}
</script> 
.container {
position: relative;
}

.countdown {  
width: 100%;  
background-color: #ffffff;  
text-align: center;  
display: flex;  
position: fixed;  
left: 0;  
top: -40px;  
justify-content: center;  
align-items: center;  
}

.mask {  
width: 100%;  
height: 100%;  
display: flex;  
justify-content: center;  
align-items: center;  
position: fixed;  
/* top: -70px; */  
left: 0;  
background-color: rgba(0, 0, 0, 0.9);  
filter: alpha(opacity=90);  
}

.mask-content {  
display: flex;  
width: 200px;  
border-radius: 20rpx;  
justify-content: center;  
align-items: center;  
padding: 40rpx;  
background-color: #ffffff;  
}
9 回复

提供可以复现的demo,方便排查


<template> <view class="container"> <cover-view class="countdown" v-if="studyFinish!=1"> <cover-view>您还需观看</cover-view> <cover-view style="color:#E60000;margin-left:10px;">{{formatSeconds(this.usedSecond)}}{{isVerifyFace}} </cover-view> </cover-view> <web-view :src="pdfSrc"></web-view> <cover-view class="mask" v-if="isVerifyFace"> <cover-view class="mask-content"> 测试 </cover-view> </cover-view> </view> </template>
<script> export default { data() { return { isVerifyFace: false, // 是否检测人脸 countDownInterval: 0, // 定时器 pdfSrc: '', // pdf文件地址 } }, onShow() { }, onLoad(option) { this.isVerifyFace=true setTimeout(()=>{ this.isVerifyFace=false },2000)
</script>
<style> .container { position: relative; }
</style>

你好,解决了吗

uni-app 中,当你同时使用 cover-viewweb-view 组件,并且通过 v-if 控制它们的显示与隐藏时,可能会遇到 v-iftrue 转为 false 时,组件未完全隐藏的问题。这通常是由于 cover-viewweb-view 的渲染机制导致的。

问题原因

cover-view 是覆盖在原生组件之上的视图容器,而 web-view 是一个原生组件。原生组件的渲染和普通组件的渲染机制不同,可能会导致 v-if 切换时,cover-view 无法正确隐藏。

解决方案

以下是几种可能的解决方案:

1. 使用 v-show 替代 v-if

v-show 是通过 CSS 的 display 属性来控制组件的显示与隐藏,而不是直接销毁和重建组件。这样可以避免 v-if 切换时可能带来的渲染问题。

<template>
  <view>
    <cover-view v-show="isVisible">Cover View Content</cover-view>
    <web-view v-show="isVisible" src="https://example.com"></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isVisible: true
    };
  }
};
</script>

2. 使用 key 强制重新渲染

通过给 cover-viewweb-view 添加 key 属性,可以在 v-if 切换时强制重新渲染组件。

<template>
  <view>
    <cover-view v-if="isVisible" :key="isVisible">Cover View Content</cover-view>
    <web-view v-if="isVisible" :key="isVisible" src="https://example.com"></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isVisible: true
    };
  }
};
</script>

3. 使用 z-index 控制层级

如果 cover-view 未完全隐藏,可能是因为它的 z-index 设置过高,导致它仍然显示在其他内容之上。你可以尝试调整 cover-viewz-index 值,确保它在隐藏时不会遮挡其他内容。

<template>
  <view>
    <cover-view v-if="isVisible" style="z-index: 1;">Cover View Content</cover-view>
    <web-view v-if="isVisible" src="https://example.com"></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isVisible: true
    };
  }
};
</script>

4. 使用 setTimeout 延迟隐藏

在某些情况下,v-if 切换时可能需要一些时间来完全隐藏组件。你可以使用 setTimeout 延迟隐藏操作,确保组件有足够的时间进行渲染和隐藏。

<template>
  <view>
    <cover-view v-if="isVisible">Cover View Content</cover-view>
    <web-view v-if="isVisible" src="https://example.com"></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isVisible: true
    };
  },
  methods: {
    hideComponents() {
      this.isVisible = false;
      setTimeout(() => {
        // 其他操作
      }, 100); // 延迟 100ms
    }
  }
};
</script>
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!