当前位置: 首页 > news >正文

uni-app x开发商城系统,资讯列表结构,数据渲染,news-item组件封装

一、概述

上一篇文章,已经实现了社区图片右侧数据渲染,预览图片。

接下来,实现资讯列表结构,数据渲染,news-item组件封装

效果如下:

 image

二、资讯列表结构

修改 pages/news/news.uvue文件,固定一行数据

完整代码如下:

<template><view><!-- 资讯 --><view class="news"><view class="news_item"><image src="https://picsum.photos/600/400?random=1"></image><view class="right"><view class="title">1季度多家房企利润跌幅超50% 去库存促销战打响</view><view class="info"><text>发表时间:2015-04-16</text><text>浏览: 3</text></view></view></view></view></view>
</template><script>export default {data() {return {}},methods: {}}
</script><style lang="scss">.news {.news_item {display: flex;padding: 10rpx 20rpx;border-bottom: 1px solid $shop-color;// 子元素自动水平排列flex-direction: row;image {width: 200rpx;height: 150rpx;min-width: 200rpx;max-width: 200rpx;}.right {// 占满剩余宽度flex: 1;margin-left: 15rpx;// 与图片同高
                height: 150rpx;display: flex;// 弹性布局 侧轴显示flex-direction: column;// 两边对齐justify-content: space-between;.title {font-size: 30rpx;}.info {display: flex;flex-direction: row;text {font-size: 24rpx;}text:nth-child(2) {margin-left: 40rpx;}}}}}
</style>

编译运行,效果如下:

image

三、数据渲染

数据获取

调用api接口

async getNews() {const res = await this.$http.get('/api/getnewslist', {})this.newsList = res.message;console.log("资讯数据", this.newsList);
},

数据渲染

修改 pages/news/news.uvue文件,增加变量newsList,进行for循环

完整代码如下:

<template><view><!-- 资讯 --><view class="news"><view class="news_item" v-for="(item,index) in newsList" :key="item.id"><image :src="item.img_url"></image><view class="right"><view class="title">{{item.title}}</view><view class="info"><text>发表时间:{{cut_data(item.add_time)}}</text><text>浏览: {{item.click}}</text></view></view></view></view></view>
</template><script>export default {data() {return {newsList: []}},onLoad() {this.getNews()},methods: {async getNews() {const res = await this.$http.get('/api/getnewslist', {})this.newsList = res.message;console.log("资讯数据", this.newsList);},// 截取日期
            cut_data(time_str) {const date = time_str.split('T')[0];return date;}}}
</script><style lang="scss">.news {.news_item {display: flex;padding: 10rpx 20rpx;border-bottom: 1px solid $shop-color;// 子元素自动水平排列flex-direction: row;image {width: 200rpx;height: 150rpx;min-width: 200rpx;max-width: 200rpx;}.right {// 占满剩余宽度flex: 1;margin-left: 15rpx;// 与图片同高
                height: 150rpx;display: flex;// 弹性布局 侧轴显示flex-direction: column;// 两边对齐justify-content: space-between;.title {font-size: 30rpx;}.info {display: flex;flex-direction: row;text {font-size: 24rpx;}text:nth-child(2) {margin-left: 40rpx;}}}}}
</style>

编译运行,效果如下:

image

四、news-item组件封装

如果别的页面,也需要显示资讯列表,再重写一遍代码,比较浪费。

因此我们可以将资讯列表数据,封装成一个组件, news-item

 

在项目根目录的components下,创建文件夹news-item,然后在里面创建文件news-item.uvue

目录结构如下:

components
├── goods-list
│   └── goods-list.uvue
└── news-item└── news-item.uvue

 

将 pages/news/news.uvue文件里面的部分内容,copy到news-item.uvue

news-item.uvue完整内容如下:

<template><view><view class="news_item" v-for="(item,index) in newsList" :key="item.id"><image :src="item.img_url"></image><view class="right"><view class="title">{{item.title}}</view><view class="info"><text>发表时间:{{cut_data(item.add_time)}}</text><text>浏览: {{item.click}}</text></view></view></view></view>
</template><script>export default {//接收父组件传递的值props: ['newsList'],methods: {// 截取日期
            cut_data(time_str) {const date = time_str.split('T')[0];return date;}}}
</script><style lang="scss">.news_item {display: flex;padding: 10rpx 20rpx;border-bottom: 1px solid $shop-color;// 子元素自动水平排列flex-direction: row;image {width: 200rpx;height: 150rpx;min-width: 200rpx;max-width: 200rpx;}.right {// 占满剩余宽度flex: 1;margin-left: 15rpx;// 与图片同高
            height: 150rpx;display: flex;// 弹性布局 侧轴显示flex-direction: column;// 两边对齐justify-content: space-between;.title {font-size: 30rpx;}.info {display: flex;flex-direction: row;text {font-size: 24rpx;}text:nth-child(2) {margin-left: 40rpx;}}}}
</style>

 

最后将 pages/news/news.uvue文件里面的部分内容删除掉,引用news-item组件

news.uvue完整内容如下:

<template><view><!-- 资讯 --><view class="news"><news-item :newsList="newsList"></news-item></view></view>
</template><script>import newsPageDataList from '../../components/news-item/news-item.uvue'export default {components: {"news-item": newsPageDataList},data() {return {newsList: []}},onLoad() {this.getNews()},methods: {async getNews() {const res = await this.$http.get('/api/getnewslist', {})this.newsList = res.message;console.log("资讯数据", this.newsList);},}}
</script><style lang="scss">.news {}
</style>

说明:

这里引用news-item组件,并传递参数newsList

 

重新编译,效果如下:

image

 

http://www.zskr.cn/news/38833.html

相关文章:

  • PostgreSQL数据库:新手开启从0到1的学习之路
  • nfs 自动挂载的一些问题
  • 2025年浙江轻奶茶加盟渠道权威推荐榜单:奶茶加盟/茶饮加盟/奶茶店加盟渠道精选
  • 2025年河南心理健康咨询机构权威推荐:河南婚姻心理咨询/河南家庭心理咨询/河南心理咨询机构服务中心精选
  • 面试:安全框架与安全管理-网络-防火墙与IPS - 徐正柱
  • 2025 年 11 月 DALI 调光系统厂家推荐排行榜,调光网关/调光开关/调光电源/调光驱动/调光传感器/调光模块/调光控制系统公司推荐
  • 2025年北京合同纠纷律师事务所权威推荐榜:专业律师团队与高效解决方案口碑之选
  • SQL - JOIN 中关联条件和过滤条件的执行顺序
  • 关于combinational and sequential parts of an fsm described in same always block ,spyglass警告
  • 记录一次数据恢复,mysql8 - 义美
  • 2025年新能源水冷电机壳铝合金浇铸机批发厂家权威推荐榜单:户外围墙配件铝合金浇铸机/厨具锅铝合金浇铸机/手套模具铝合金浇铸机源头厂家精选
  • 2025 年 11 月石墨烯,可膨胀石墨,导热石墨母粒厂家最新推荐,产能、专利、环保三维数据透视!
  • Cisco Jabber 15.1 (Andriod, iOS, macOS, Windows) - 面向企业的多合一通信工具
  • 2025年青石栏杆制造厂权威推荐榜单:别墅石栏杆/石栏杆/河道石栏杆源头厂家精选
  • 2025年手动叠片过滤器生产厂家权威推荐榜单:全自动反冲洗叠片过滤器/离心过滤器/钢制离心过滤器设备源头厂家精选
  • 企业热线电话系统的多渠道支持与服务拓展策略!
  • DockerDeskTop安装常用的中间件
  • 2025高性价比Facebook广告品牌企业TOP5推荐:精准引流与高效转化的权威测评指南
  • 如何优化机器人拨打电话软件的通话效率?实用技巧!
  • 2025 年工业商城小程序最新推荐排行榜:涵盖多领域设备,解析实力企业核心优势与选择要点节能环保/车间工具/智能制造/数控转台工业商城推荐
  • SAP 字段名+RANGE表
  • 系统关键信息收集
  • 2025年金属线材加工设备企业排名:江苏优轧机械有限公司
  • 成都恒利泰滤波器HT-LFCG-490+
  • Photoshop 新伴侣!ACR 2026 五大新功能实战教学(附使用技巧)
  • 2025 年塔钟生产厂家最新推荐榜单:电子 / 户外 / 建筑等多类型塔钟品牌实测,核心机芯与服务深度解析
  • 2025年钢格栅生产商哪家好?中国十大钢格栅厂家年度排名权威测评与深度解析
  • 免登录使用 WPS Office 编辑功能
  • 2025年装配式卸落块定做厂家权威推荐榜单:桥梁落架卸落块/100吨卸落块/200吨卸落块源头厂家精选
  • 2025年黑龙江高考美术适用真题教材辅导学校TOP5排行榜,艺考生择校避坑指南