uni-app nvue中list组件少渲染数据
uni-app nvue中list组件少渲染数据
信息类别 | 详细信息 |
---|---|
产品分类 | uniapp/App |
PC开发环境操作系统 | Windows |
PC开发环境操作系统版本号 | Windows 10 专业版 |
HBuilderX类型 | 正式 |
HBuilderX版本号 | 4.14 |
手机系统 | Android |
手机系统版本号 | Android 13 |
手机厂商 | 小米 |
手机机型 | redmiK50至尊版 |
页面类型 | vue |
vue版本 | vue2 |
打包方式 | 云端 |
项目创建方式 | HBuilderX |
示例代码:
<!-- 普通模式演示(nvue) -->
<template>
<view class="content">
<list :style="{ height: height, background: '#000000' }" @loadmore="queryList(pageNum+1,5)"
:pagingEnabled="true">
<cell :style="videoStyle" class="item" v-for="(item,index) in dataList" :key="item.title"
@disappear="Disappear(item)" @click="itemClick(item)">
<video v-if="item.videoId" :poster="item.cover"
src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-2908110e-6da2-4899-8b44-d45c153457ad/ec383f81-6896-4274-8861-e329ae1376b4.mp4"></video>
<image v-else :style="videoStyle" :src="item.cover"></image>
<view class="content_ment" @appear="Appear(item)">
{{index}}
</view>
</cell>
</list>
</view>
</template>
<script>
export default {
data() {
return {
dataList: [],
height: 0,
sHeight: 0,
videoStyle: {
height: 0,
width: "750rpx",
},
videoPlayId: "",
nextId: "",
exitId: "",
currentId: "",
pageNum: 1,
}
},
watch: {
currentId(val) {
if (this.dataList.length > 3) {
if (val === this.dataList[this.dataList.length - 3].id) {
this.$nextTick(() => {
this.queryList(this.pageNum+1, 5);
});
}
}
},
},
onLoad() {
this.height = uni.getSystemInfoSync().windowHeight;
this.sHeight = uni.getSystemInfoSync().statusBarHeight;
this.videoStyle.height = this.height;
this.queryList(this.pageNum, 5);
},
methods: {
getData(params) {
return new Promise((resolve, reject) => {
uni.request({
url: '数据地址',
method: 'POST',
data: params,
success: (res) => {
resolve(res.data.data);
},
fail() {
reject(undefined)
}
})
})
},
async queryList(pageNo, pageSize) {
this.pageNum = pageNo;
const params = {
pageNum: pageNo,
pageSize: pageSize,
}
const res = await this.getData(params);
this.dataList = this.dataList.concat(res.list)
},
Appear(data) {
this.nextId = data.id;
},
Disappear(data) {
this.exitId = data.id;
if (this.exitId !== this.nextId) {
this.currentId = this.nextId;
this.videoPlayId = this.dataList.filter(
(item) => item.id === this.nextId,
)[0].id;
}
},
itemClick(item) {
console.log('点击了', item.title);
}
}
}
</script>
<style>
.item {
flex-direction: row;
position: relative;
align-items: center;
justify-content: space-between;
border: 1prx solid red;
position: relative;
}
.item-detail {
padding: 5rpx 15rpx;
border-radius: 10rpx;
font-size: 28rpx;
color: white;
background-color: #007AFF;
}
.item-line {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 1px;
background-color: #eeeeee;
}
.content_ment {
position: absolute;
right: 26rpx;
bottom: 632rpx;
width: 100rpx;
height: 100rpx;
background-color: #007AFF;
}
.home-video-player-container {
width: 750rpx;
}
</style>
操作步骤:
分页获取数据,最好是每页几条数据(数据类型中含有视频和图片)
在列表中根据类型渲染出数据即可
预期结果:
数据正常显示
实际结果:
数据少渲染,导致页面卡中间
bug描述:
代码大概是这样,刷着刷着就少渲染一条数据,就会卡在页面中间
更多关于uni-app nvue中list组件少渲染数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
4 回复
我也顶一顶
看看呢
在 uni-app
的 nvue
中,list
组件(通常是 scroll-view
或 list
组件)如果出现少渲染数据的情况,可能是由于以下几个原因导致的。以下是一些常见的排查和解决方法:
1. 数据未正确更新
确保你的数据源已经正确更新,并且在数据更新后触发了重新渲染。如果数据源没有正确更新,list
组件可能不会渲染所有的数据。
解决方法:
- 检查数据源是否已经更新。
- 使用
Vue.set
或this.$set
来确保数据更新时触发响应式更新。 - 在数据更新后,手动调用
this.$forceUpdate()
强制组件重新渲染。
2. list
组件的 height
或 style
设置不正确
如果 list
组件的高度设置不正确,可能会导致部分数据无法显示。
解决方法:
- 确保
list
组件的高度设置正确,能够容纳所有数据。 - 使用
flex: 1
或者height: 100%
来确保list
组件充满父容器。
<scroll-view style="flex: 1;">
<!-- 列表内容 -->
</scroll-view>
3. list
组件的 scroll
事件未正确触发
如果 list
组件的 scroll
事件未正确触发,可能会导致数据加载不完整。
解决方法:
- 确保
scroll
事件已经正确绑定,并且在滚动时触发数据加载。 - 使用
[@scrolltolower](/user/scrolltolower)
事件来加载更多数据。
<scroll-view [@scrolltolower](/user/scrolltolower)="loadMore">
<!-- 列表内容 -->
</scroll-view>
4. list
组件的 key
设置不正确
如果 list
组件的 key
设置不正确,可能会导致渲染错误或部分数据未渲染。
解决方法:
- 确保每个列表项的
key
是唯一的,并且能够正确标识每个项。
<scroll-view>
<view v-for="(item, index) in list" :key="item.id">
{{ item.name }}
</view>
</scroll-view>
5. list
组件的性能优化问题
nvue
中的 list
组件为了提高性能,可能会对渲染进行优化,导致部分数据未渲染。
解决方法:
- 使用
nvue
的recycle-list
组件来优化长列表的渲染性能。 - 确保列表项的高度固定,以便
recycle-list
能够正确计算和渲染。
<recycle-list :list="list" :item-height="100">
<template v-slot="{ item }">
<view>{{ item.name }}</view>
</template>
</recycle-list>
6. 数据量过大导致渲染问题
如果数据量过大,可能会导致 list
组件无法一次性渲染所有数据。
解决方法:
- 使用分页加载或懒加载的方式,分批加载数据。
- 使用
scroll-view
的lower-threshold
属性来控制加载更多的触发时机。
<scroll-view [@scrolltolower](/user/scrolltolower)="loadMore" lower-threshold="100">
<!-- 列表内容 -->
</scroll-view>