uni-app文章列表制作⑨

文章目录

十三、文章列表制作-文章卡片

一、创建文章相关 listItem 组件

使用 scroll-view 实现竖向滚动容器制作,注意在样式定义时进行多级高度设定

vue 复制代码
<view class="list-scroll-container">
		<scroll-view scroll-y="true" class="list-scroll">
			<view>
				<ListCard v-for="item in 50" :key="item"></ListCard>
			</view>
		</scroll-view>
</view>
二、文章卡片组件定义
  1. 创建基本样式及结构
  2. 定义多状态卡片模块
    1. 通过父组件传递 mode 属性进行条件编译
    2. 根据条件进行选项卡卡片展示
三、定义 uniapp 模版
  1. 根目录下创建 index.html 文件
  2. 参考地址:https://uniapp.dcloud.io/collocation/manifest?id=h5-template
  3. manifest 文件的 html5 配置中进行 index.html 文件引入

十四、文章列表制作-数据渲染

一、云函数创建

定义 articleList 云函数

删除不需要返回的文章详情内容

react 复制代码
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	const list = await db.collection('article')
	.aggregate()
	.project({
		content:0
	})
	.end()
	//返回数据给客户端
	return {
		code:0,
		msg:"数据请求成功",
		data:list.data
	}
};
二、前端进行数据获取
  1. articleList 组件 => created 钩子中进行数据获取

    react 复制代码
    created () {
        this._getArticleList()
      }
     methods: {
        async _getArticleList () {
          const articleList = await this.$http.get_article_list()
          this.articleList = articleList
        }
      }
三、数据渲染

listItem 组件中进行循环渲染使用

react 复制代码
<view class="list-scroll-container">
		<scroll-view scroll-y="true" class="list-scroll">
			<view>
				<ListCard :item="item" v-for="(item,index) in articleList" :key="index"></ListCard>
			</view>
		</scroll-view>
	</view>
四、根据选项卡分类进行数据渲染
  1. 添加全部分类选项

    react 复制代码
    async _intiLabelList() {
    				const labelList = await this.$http.get_label_list()
    				this.labelList = [{name:"全部"},...labelList]
    			}
  2. 请求 articleList 时进行数据传递

    1. 为了保证导航数据的正确获取,调整 created 函数的_getArticle_list 方法在 watch 中进行调用

      react 复制代码
       watch: {
          labelList(newVal,OldVal) {
          this._getArticleList()
          },
        },
  3. 云函数进行数据过滤调整

    js 复制代码
    "use strict";
    const db = uniCloud.database();
    exports.main = async (event, context) => {
      const { classify } = event;
    
      let matchObj = {};
    
      if (classify !== "全部") {
        matchObj = { classify };
      }
    
      const list = await db
        .collection("article")
        .aggregate() // 使用聚合的形式进行数据的获取
        .match(matchObj) // 根据匹配条件进行数据返回
        .project({
          content: 0, // 本次查询不需要返回文章详情给前端
        })
        .end();
      //返回数据给客户端
      return {
        code: 0,
        msg: "数据请求成功",
        data: list.data,
      };
    };
  4. 前端对数据进行缓存处理

    将原有接受内容数组转换为对象进行存储。每次 change 改变内容时进行判断操作处理

    使用$set 方法进行对象的页面重新渲染

    js 复制代码
    async _getArticleList (currentIndex) {
          const articleList = await this.$http.get_article_list({classify:this.labelList[currentIndex].name})
          this.$set(this.articleData,currentIndex,articleList)
        }
相关推荐
宸翰18 小时前
uni-app 设置 Android 底部虚拟导航栏背景色
android·前端·uni-app
千逐6821 小时前
UniApp 鸿蒙实战:音视频与多媒体处理完全指南
华为·uni-app·harmonyos·鸿蒙
handsome091621 小时前
windows开发uniapp如何上架app到app store
uni-app·上架
宠友信息1 天前
Redis 内容社区源码架构优化与即时通讯数据一致性处理
spring boot·后端·websocket·mysql·uni-app
CHB2 天前
uni-app x 蒸汽模式 iOS版 性能测试基准报告 Benchmark
ios·uni-app·swiftui
衍生星球2 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
编程猪猪侠3 天前
UniAppx安卓app实现左侧导航与右侧内容联动滚动
android·uni-app
千逐683 天前
Uniapp 鸿蒙实战之 AtomGit APP - 仓库详情与文件树浏览
服务器·microsoft·uni-app
万物得其道者成3 天前
uni-app App 热更新实现指南:从接口检查到下载安装重启(附带AI提示词)
uni-app
anyup3 天前
uView Pro 正式支持 MCP 协议!让 AI 秒懂你的组件库
前端·uni-app·ai编程