uni-app 报Bug:打包结果与真机测试结果不同,web-view适配问题。使用方法在真机测试时有效,能适配各种屏幕,但打包安装使用后,web-view铺满屏幕,无法适配。

uni-app 报Bug:打包结果与真机测试结果不同,web-view适配问题。使用方法在真机测试时有效,能适配各种屏幕,但打包安装使用后,web-view铺满屏幕,无法适配。

开发环境 版本号 项目创建方式
Windows win10 HBuilderX

操作步骤:

// #ifdef APP-PLUS
var currentWebview = this.$scope.$getAppWebview();
const query = uni.createSelectorQuery().in(this);
let topHeight = 0;
query.select(’.header-top-bg’).boundingClientRect((data) => {
topHeight = data.height; // 头部元素高度
}).exec();
let hight = uni.getSystemInfoSync()
this.screenHeight = hight.screenHeight
console.log(this.screenHeight)
let mate = hight.screenHeight - 70
let wv = currentWebview.children()[0];
console.log(mate)
console.log(this.appHight)
this.$nextTick(() =>{
wv.setStyle({top:topHeight,height: mate})
})
// #endif


### 预期结果:

根据各种屏幕适配


### 实际结果:

没有效果


### bug描述:
<template> <view class="container"> <view class="main"> <web-view :src="src" class="webBox"></web-view> </view> <foot :current-page = "4" class="tabbar" v-if="footShow"></foot> </view> </template> <script> import footer from '../../component/footer.vue' export default { components:{ foot:footer }, data() { return { src: 'https://www.720yun.com/vr/6ffjzzsukm0', footShow:true, screenHeight:'' } }, mounted() { this.$music.playBgm({mute:false}) }, onLaunch() { uni.hideTabBar() }, onShow() { this.footShow = true }, onHide() { // this.footShow = false }, async onReady(){ // #ifdef APP-PLUS var currentWebview = this.$scope.$getAppWebview(); const query = uni.createSelectorQuery().in(this); let topHeight = 0; query.select('.header-top-bg').boundingClientRect((data) => { topHeight = data.height; // 头部元素高度 }).exec(); let hight = uni.getSystemInfoSync() this.screenHeight = hight.screenHeight console.log(this.screenHeight) let mate = hight.screenHeight - 70 let wv = currentWebview.children()[0]; console.log(mate) console.log(this.appHight) this.$nextTick(() =>{ wv.setStyle({top:topHeight,height: mate}) }) // #endif }, methods: { } } </script> <style scoped> .container { display: flex; flex-direction: column; height: 100%; width: 100%; background-color: #052d3d; } .main{ height: 95%; width: 100%; flex: 1; } .tabbar{ width: 100%; height: 70px; z-index: 9; } </style>

更多关于uni-app 报Bug:打包结果与真机测试结果不同,web-view适配问题。使用方法在真机测试时有效,能适配各种屏幕,但打包安装使用后,web-view铺满屏幕,无法适配。的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 报Bug:打包结果与真机测试结果不同,web-view适配问题。使用方法在真机测试时有效,能适配各种屏幕,但打包安装使用后,web-view铺满屏幕,无法适配。的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在 uni-app 中,web-view 组件的适配问题可能会在真机测试和打包后出现不一致的情况。以下是可能的原因和解决方法:

1. 检查 web-view 的样式

确保 web-view 的样式正确设置,尤其是在不同屏幕尺寸下的适配。你可以使用 flex 布局或 vh/vw 单位来确保 web-view 能够根据屏幕尺寸自适应。

<template>
  <view class="container">
    <web-view :src="url" class="web-view"></web-view>
  </view>
</template>

<style>
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.web-view {
  flex: 1;
  width: 100%;
}
</style>

2. 检查 web-viewsrc 路径

确保 web-viewsrc 路径正确,并且在打包后仍然有效。如果路径是动态生成的,确保在打包后路径仍然正确。

3. 打包后的样式差异

打包后可能会出现样式差异,可能是因为打包工具对样式进行了压缩或优化。你可以尝试在 manifest.json 中禁用样式压缩:

{
  "app-plus": {
    "css": {
      "minify": false
    }
  }
}

4. 检查 web-view 的父容器

确保 web-view 的父容器没有设置固定的高度或宽度,或者设置了不正确的样式,导致 web-view 无法正确适配。

5. 使用 uni.getSystemInfoSync 动态设置高度

你可以使用 uni.getSystemInfoSync 获取设备的屏幕高度,然后动态设置 web-view 的高度。

export default {
  data() {
    return {
      url: 'https://example.com',
      webViewHeight: 0
    }
  },
  onLoad() {
    const systemInfo = uni.getSystemInfoSync()
    this.webViewHeight = systemInfo.windowHeight
  }
}
<template>
  <view class="container">
    <web-view :src="url" :style="{ height: webViewHeight + 'px' }"></web-view>
  </view>
</template>

6. 检查 web-view 的内嵌内容

如果 web-view 中加载的内容本身有样式问题,可能会导致显示不正常。你可以尝试在 web-view 中加载一个简单的 HTML 页面,看看问题是否依然存在。

7. 使用 [@media](/user/media) 查询

使用 [@media](/user/media) 查询来针对不同的屏幕尺寸设置不同的样式。

[@media](/user/media) (max-width: 600px) {
  .web-view {
    height: 100vh;
  }
}

8. 检查 uni-app 版本

确保你使用的 uni-app 版本是最新的,或者至少是一个稳定的版本。旧版本可能存在一些已知的 Bug,更新到最新版本可能会解决问题。

9. 查看打包日志

在打包时,查看是否有任何警告或错误信息,可能会提示问题的根源。

10. 使用 uni-app 官方社区或论坛

如果以上方法都无法解决问题,可以尝试在 uni-app 的官方社区或论坛中寻求帮助,可能会有其他开发者遇到过类似的问题。

11. 使用 web-viewuser-scalable 属性

如果问题与缩放有关,可以尝试在 web-viewmeta 标签中设置 user-scalable=no

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
回到顶部