uni-app框架 微信小程序页面显示正常,但安卓页面样式显示异常

这篇具有很好参考价值的文章主要介绍了uni-app框架 微信小程序页面显示正常,但安卓页面样式显示异常。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

问题

今天在继续复习uni-app项目时,使用模拟器运行时,突然发现封装的search组件样式无法正常显示,但是小程序页面又是正常的,打包后真机也是一样的结果。在uni-app的控制台报如下错误:
[Vue warn]: Error in render: “TypeError: Cannot read property ‘children’ of undefined”
TypeError: Cannot read property ‘children’ of undefined

问题截图

小程序界面搜索样式正常
uni-app运行到安卓模拟器展示不出来,uni-app,微信小程序,android
安卓界面运行异常
uni-app运行到安卓模拟器展示不出来,uni-app,微信小程序,android

问题分析

这是分类页面中的源码,其中my-search组件即为上方图中的固定定位搜索框

<template>
  <view>
    <my-search ></my-search>
    <view class="scrollContainer" >
      <scroll-view scroll-y="true" class="leftScrollContainer" :style="{height: scrollHeight + 'px' }" :scroll-top="leftScrollTop"> 
        <view :class="'firstCateItem ' + (index===currentSelectedIndex? 'active' :'') " v-for="(item1,index) in cateList" :key="index" @click="changeSelectedIndex(index)">
          {{item1.cat_name}}
        </view>
      </scroll-view>
    <scroll-view scroll-y="true" class="rightScrollContainer" :style="{height: scrollHeight + 'px'}" :scroll-top="rightScrollTop">
        <view class="childCateContainer" v-for="(item2,index2) in cateList[currentSelectedIndex].children " :key="index2">
          <view>
           <text class="secondCate">/ {{item2.cat_name}} /</text>
        <view class="thirdCateContainer" >
             <view class="thirdCateItem" v-for="(item3,index3) in item2.children" :key="index3" @click="goToGoodList(item3)">
               <image :src="item3.cat_icon" mode=""></image>
               <text>{{item3.cat_name}}</text>
             </view>
           </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>
<script>
import bus from "@/utils/bus";
  export default {
    data() {
      return {
        scrollHeight:'',
        cateList:[],
        currentSelectedIndex:0,
        rightScrollTop:0,
        leftScrollTop:0,
      };
    },
    created(){
      bus.on('resetScroll', () => {
        this.currentSelectedIndex = 0
        this.leftScrollTop = this.leftScrollTop ? 0 : 1
        this.rightScrollTop = this.rightScrollTop === 0 ? 1 : 0
      })
    },
    onUnload: () => {
      bus.off('resetScroll')
    },
    onLoad() {
      this.getViewUsedHeight()
      this.getCateList()
      console.log("哈哈哈",this.cateList);
    },
    methods:{
      goToGoodList(item3){
        console.log(item3);
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
        })
      },
      changeSelectedIndex(index){
        this.currentSelectedIndex = index
        // this.secondCateList = this.cateList[index].children
        this.rightScrollTop = this.rightScrollTop === 0 ? 1 : 0
      },
      getViewUsedHeight(){
        uni.getSystemInfo({
          success: (res) => {
            console.log("这是获取到的设备信息:",res);
            if(res.errMsg === "getSystemInfo:ok" && res.windowHeight){
              uni.$showMsg("窗口可用高度为"+res.windowHeight)
              this.scrollHeight = res.windowHeight - 60
            }else{
              return uni.$showMsg("获取当前屏幕高度失败!")
            }
          },fail: (err) => {
            console.log("获取屏幕高度失败,信息为:",err);
          }
        })
      },
      async getCateList(){
        const {data:res} = await uni.$http.get('/api/public/v1/categories')
        console.log("这是获取到的分类数据:",res);
        if(res.meta.status !== 200) return uni.$showMsg("没有成功获取到分类数据!")
        this.cateList = res.message
        console.log("获取分类的请求已经结束了!!!");
        // this.secondCateList = this.cateList[0].children
        console.log(this.cateList);
      }
    }
  }
</script>

在控制台中报了这样的错误:

[Vue warn]: Error in render: “TypeError: Cannot read property ‘children’ of undefined”
TypeError: Cannot read property ‘children’ of undefined

之前第一次写的时候也没问题,就没处理,但这次出了问题,只好从这个提示入手。

分析过程:

1.根据提示显然是使用v-for循环页面渲染二级和三级分类使用一级分类cateList对象里面的数据即cateList[currentSelectedIndex].children 时出了问题,但是思考了半天没找到问题所在,我在请求数据的时候就已经将获取到的数据赋值给了data函数中的cateList身上,currentSelectedIndex也在data函数中默认给了定值0。

2.查阅网上资料说是,页面在第一次渲染时数据还没有拿到造成的,使用v-if,判断要渲染的页面数据
是否存在,不存在时不渲染,根据提示我使用了v-if将cateList[currentSelectedIndex].children放在了class="rightScrollContainer"的scroll-view标签上,但是依然无效,报错依旧。

3.后面查阅了项目文档,确实是Vue在第一次渲染页面时拿不到数据导致。于是我修改了使用属性嵌套进行页面渲染的方法,将cateList[currentSelectedIndex].children改为变量名secondCateList声明在data函数中,并在onLoad函数中调用this.getCateList()方法获取分类数据后,将cateList[0].children的值赋值给sendCondList的变量放到页面去渲染,但是报错依旧。

4.后面联想到了Vue的声明周期,并且根据AI的提示,我尝试在onLoad函数中打印请求返回的数据this.cateList值的情况,主要源码和运行截图如下:

    onLoad() {
      this.getViewUsedHeight()
      this.getCateList()
      console.log("这是onLoad函数中当前this.cateList的值:",this.cateList);
    },
    async getCateList(){
        const {data:res} = await uni.$http.get('/api/public/v1/categories')
        console.log("这是获取到的分类数据:",res);
        if(res.meta.status !== 200) return uni.$showMsg("没有成功获取到分类数据!")
        this.cateList = res.message
        console.log("获取分类的请求已经结束了!!!");
      }

uni-app运行到安卓模拟器展示不出来,uni-app,微信小程序,android
可以看到,在onLoad函数中的请求方法this.getCateList()运行结束之前,onLoad函数体中的最后一行打印语句就已经执行了,这也解释了为什么我在请求函数"结束"之后修改值依然无效,页面渲染报前面提到的错的原因。

解决方法:

知道了原因,我们就可以将二级分类数据secondCateList声明在computed之中,并且使用三元表达式判断它的值是否存在,不存在时返回一个空数组,这样Vue在页面首次加载时就不会去渲染了,报错消失后,这个小程序页面样式正常,安卓样式异常的问题就解决了。

修改后的页面源码和运行截图如下:

<template>
  <view>
    <my-search ></my-search>
    <view class="scrollContainer" >
      <scroll-view scroll-y="true" class="leftScrollContainer" :style="{height: scrollHeight + 'px' }" :scroll-top="leftScrollTop"> 
        <view :class="'firstCateItem ' + (index===currentSelectedIndex? 'active' :'') " v-for="(item1,index) in cateList" :key="index" @click="changeSelectedIndex(index)">
          {{item1.cat_name}}
        </view>
      </scroll-view>
    <scroll-view scroll-y="true" class="rightScrollContainer" :style="{height: scrollHeight + 'px'}" :scroll-top="rightScrollTop">
        <view class="childCateContainer" v-for="(item2,index2) in secondCateList " :key="index2">
          <view>
           <text class="secondCate">/ {{item2.cat_name}} /</text>
        <view class="thirdCateContainer" >
             <view class="thirdCateItem" v-for="(item3,index3) in item2.children" :key="index3" @click="goToGoodList(item3)">
               <image :src="item3.cat_icon" mode=""></image>
               <text>{{item3.cat_name}}</text>
             </view>
           </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import bus from "@/utils/bus";
  export default {
    data() {
      return {
        scrollHeight:'',
        cateList:[],
        currentSelectedIndex:0,
        rightScrollTop:0,
        leftScrollTop:0,
      };
    },
    computed:{
      secondCateList(){
        //必须要设置一个空数组,在第一次渲染的时候,this.cateList是没有的,再调用它的子属性children会报错,虽然小程序不影响结果,但是安卓上会导致页面样式渲染出现异常
        return this.cateList[this.currentSelectedIndex] ? this.cateList[this.currentSelectedIndex].children : []
      }
    },
    created(){
      bus.on('resetScroll', () => {
        this.currentSelectedIndex = 0
        this.leftScrollTop = this.leftScrollTop ? 0 : 1
        this.rightScrollTop = this.rightScrollTop === 0 ? 1 : 0
      })
    },
    onUnload: () => {
      bus.off('resetScroll')
    },
    onLoad() {
      this.getViewUsedHeight()
      this.getCateList()
      console.log("这是onLoad函数中当前this.cateList的值:",this.cateList);
    },
    methods:{
      goToGoodList(item3){
        console.log(item3);
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
        })
      },
      changeSelectedIndex(index){
        this.currentSelectedIndex = index
        // this.secondCateList = this.cateList[index].children
        this.rightScrollTop = this.rightScrollTop === 0 ? 1 : 0
      },
      getViewUsedHeight(){
        uni.getSystemInfo({
          success: (res) => {
            console.log("这是获取到的设备信息:",res);
            if(res.errMsg === "getSystemInfo:ok" && res.windowHeight){
              uni.$showMsg("窗口可用高度为"+res.windowHeight)
              this.scrollHeight = res.windowHeight - 60
            }else{
              return uni.$showMsg("获取当前屏幕高度失败!")
            }
          },fail: (err) => {
            console.log("获取屏幕高度失败,信息为:",err);
          }
        })
      },
      async getCateList(){
        const {data:res} = await uni.$http.get('/api/public/v1/categories')
        console.log("这是获取到的分类数据:",res);
        if(res.meta.status !== 200) return uni.$showMsg("没有成功获取到分类数据!")
        this.cateList = res.message
        console.log("获取分类的请求已经结束了!!!");
      }
    }
  }
</script>

uni-app运行到安卓模拟器展示不出来,uni-app,微信小程序,android

总结
刚学完uni-app时,看到有人说这个框架就是一端运行多端报错还有点怀疑,这下不得不信了。不过也有原因在于我当时没有去处理复杂对象中的嵌套数据在页面渲染错误所导致的,同时也重新认识了下onLoad函数体语句和异步请求方法的执行顺序问题。总而言之,在使用接口返回的复杂数据渲染页面时,优先使用computed计算属性,结合三元表达式去判断要渲染的数据源是否已经存在,不存在应给定空数组或对象,以避免一端运行,多端报错的可能。文章来源地址https://www.toymoban.com/news/detail-847297.html

到了这里,关于uni-app框架 微信小程序页面显示正常,但安卓页面样式显示异常的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Uni-app实现左右滑动页面内容切换(兼容微信小程序)

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档         前言         整体思路         一、HTML部分         二、Script部分         三、Style部分           (先声明哦我可不是偷懒,只是想学术借鉴一下)因为最近有在做左右滑动功能,

    2024年02月07日
    浏览(56)
  • uni-app 微信小程序之好看的ui登录页面(四)

    更多登录ui页面 uni-app 微信小程序之好看的ui登录页面(一) uni-app 微信小程序之好看的ui登录页面(二) uni-app 微信小程序之好看的ui登录页面(三) uni-app 微信小程序之好看的ui登录页面(四) uni-app 微信小程序之好看的ui登录页面(五)

    2024年02月04日
    浏览(31)
  • uni-app 微信小程序之好看的ui登录页面(二)

    更多登录ui页面 uni-app 微信小程序之好看的ui登录页面(一) uni-app 微信小程序之好看的ui登录页面(二) uni-app 微信小程序之好看的ui登录页面(三) uni-app 微信小程序之好看的ui登录页面(四) uni-app 微信小程序之好看的ui登录页面(五)

    2024年02月03日
    浏览(34)
  • uni-app 微信小程序之好看的ui登录页面(三)

    更多登录ui页面 uni-app 微信小程序之好看的ui登录页面(一) uni-app 微信小程序之好看的ui登录页面(二) uni-app 微信小程序之好看的ui登录页面(三) uni-app 微信小程序之好看的ui登录页面(四) uni-app 微信小程序之好看的ui登录页面(五)

    2024年01月15日
    浏览(38)
  • uni-app 微信小程序之好看的ui登录页面(一)

    更多登录ui页面 uni-app 微信小程序之好看的ui登录页面(一) uni-app 微信小程序之好看的ui登录页面(二) uni-app 微信小程序之好看的ui登录页面(三) uni-app 微信小程序之好看的ui登录页面(四) uni-app 微信小程序之好看的ui登录页面(五)

    2024年02月04日
    浏览(41)
  • uni-app 微信小程序之好看的ui登录页面(五)

    更多登录ui页面 uni-app 微信小程序之好看的ui登录页面(一) uni-app 微信小程序之好看的ui登录页面(二) uni-app 微信小程序之好看的ui登录页面(三) uni-app 微信小程序之好看的ui登录页面(四) uni-app 微信小程序之好看的ui登录页面(五)

    2024年02月04日
    浏览(38)
  • uni-app实现点击显示隐藏列表,兼容微信小程序

    效果:    小程序打印的结果:值一直为true   如果你试过v-if不生效,又试了v-show,还是不行!!千万不要着急! 不是自己写的不对,而是uni-app和微信小程序修改值的方式不一致造成的。反正就是不承认是自己的问题。 其实解决的办法也很简单,就是要兼容两端,写出符合

    2024年02月09日
    浏览(47)
  • uni-app:使用 Painter 在微信小程序将当前页面保存为图片

    手机截屏 Painter 实现 方式一:Painter Painter 是一个微信小程序组件,具体介绍和 API 请参考:GitHub文档。 在 GitHub 下载下来之后只需要将 components 下的 painter 文件夹放到项目根目录下的 wxcomponents 文件夹即可。然后就是如何在 uni-app 中使用微信小程序形式的组件,其实很简单,

    2024年02月12日
    浏览(48)
  • 微信小程序原生框架转Uni-App:你需要知道的一切

    博主 默语带您 Go to New World. ✍ 个人主页—— 默语 的博客👦🏻 《java 面试题大全》 🍩惟余辈才疏学浅,临摹之作或有不妥之处,还请读者海涵指正。☕🍭 《MYSQL从入门到精通》数据库是开发者必会基础之一~ 🪁 吾期望此文有资助于尔,即使粗浅难及深广,亦备添少许微薄

    2024年02月09日
    浏览(43)
  • 解决uni-app微信小程序底部输入框,键盘弹起时页面整体上移问题

    做了一个记录页面(类似单方聊天页),输入框在底部;当弹出键盘时,页面整体上移,页面头信息会消失不见 比如一个记录页面,需要在键盘弹出时: 底部的输入框跟随键盘上弹 页面头固定在顶部不动 聊天信息区域(即内容区)调整高度,该区域局部滚动 底部输入框f

    2024年02月13日
    浏览(93)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包