当前位置:首页 > uni-app

uniapp 内容列表

2026-01-15 18:17:39uni-app

uniapp 内容列表的实现方法

在uniapp中实现内容列表通常需要结合组件和数据绑定。以下是一个常见的方法:

模板部分

<template>
  <view class="content">
    <scroll-view scroll-y="true" class="list">
      <view v-for="(item, index) in listData" :key="index" class="list-item">
        <text>{{ item.title }}</text>
        <image :src="item.image" mode="aspectFill"></image>
      </view>
    </scroll-view>
  </view>
</template>

脚本部分

uniapp 内容列表

<script>
export default {
  data() {
    return {
      listData: [
        { title: '项目1', image: '/static/image1.jpg' },
        { title: '项目2', image: '/static/image2.jpg' }
      ]
    }
  }
}
</script>

样式部分

<style>
.content {
  padding: 20rpx;
}
.list {
  height: 100vh;
}
.list-item {
  margin-bottom: 20rpx;
  padding: 20rpx;
  background-color: #fff;
  border-radius: 10rpx;
}
</style>

分页加载的实现

对于长列表,实现分页加载可以提升性能:

uniapp 内容列表

data() {
  return {
    listData: [],
    page: 1,
    loading: false,
    noMore: false
  }
},
methods: {
  loadData() {
    if (this.loading || this.noMore) return

    this.loading = true
    uni.request({
      url: 'your_api_url',
      data: { page: this.page },
      success: (res) => {
        if (res.data.length) {
          this.listData = [...this.listData, ...res.data]
          this.page++
        } else {
          this.noMore = true
        }
      },
      complete: () => {
        this.loading = false
      }
    })
  }
},
onReachBottom() {
  this.loadData()
},
onLoad() {
  this.loadData()
}

性能优化技巧

使用<recycle-list>组件处理超长列表:

<recycle-list 
  :list="listData"
  :template-key="'templateKey'"
  :alias="'item'">
  <template v-slot:default="props">
    <view>{{ props.item.title }}</view>
  </template>
</recycle-list>

添加下拉刷新功能:

onPullDownRefresh() {
  this.page = 1
  this.noMore = false
  this.listData = []
  this.loadData()
  uni.stopPullDownRefresh()
}

常见UI库实现

使用uView UI库快速创建列表:

<template>
  <u-list>
    <u-list-item 
      v-for="(item, index) in listData"
      :key="index"
      :title="item.title"
      :thumb="item.image">
    </u-list-item>
  </u-list>
</template>

注意事项

  • 列表项必须设置唯一的:key属性
  • 长列表应考虑使用虚拟滚动技术
  • 图片加载应使用懒加载模式
  • 分页加载时需处理边界条件
  • 移动端需考虑下拉刷新和上拉加载的交互体验

标签: 内容列表
分享给朋友:

相关文章

vue实现列表循环

vue实现列表循环

列表循环的实现方式 在Vue中实现列表循环主要依赖于v-for指令,该指令基于源数据多次渲染元素或模板块。 基础数组循环 使用v-for遍历数组时,语法为item in items或(item, i…

vue实现表格内容

vue实现表格内容

Vue实现表格内容的方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签构建表格结构,通过v-for指令动态渲染数据。 <template> &…

vue实现树形列表

vue实现树形列表

Vue 实现树形列表的方法 递归组件实现 使用递归组件是最常见的方式,适合处理嵌套层级不确定的数据结构。 数据结构示例 data() { return { treeData: […

vue实现搜索内容

vue实现搜索内容

Vue实现搜索功能的方法 在Vue中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法: 使用计算属性实现搜索 计算属性非常适合处理搜索逻辑,能够根据输入的关键词动态过滤数据。 <…

vue实现分页显示内容

vue实现分页显示内容

Vue实现分页显示内容的方法 基础分页实现 在Vue中实现分页功能通常需要结合v-for指令和计算属性。定义一个currentPage变量表示当前页码,itemsPerPage表示每页显示的项目数。通…

vue 搜索框列表实现

vue 搜索框列表实现

Vue 搜索框列表实现 在 Vue 中实现搜索框列表功能,通常涉及数据绑定、过滤和渲染列表。以下是具体实现方法: 数据绑定与搜索输入 创建 Vue 实例或组件,定义数据模型和搜索输入框: <…