当前位置: 首页 > 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.jsqmd.com/news/30287/

相关文章:

  • 使用office tool plus 激活office
  • #课后作业1:课件动手动脑及验证内容解答 - 20243867孙堃2405
  • 智变未来:中国AI HR市场进程盘点与主流玩家深度分析
  • PostgreSQL数据库:新手开启从0到1的学习之路
  • 2025电线电缆生产厂家,电线电缆厂家精选:武汉特航,赋能多行业的技术型品牌揭秘!
  • nfs 自动挂载的一些问题
  • 2025年浙江轻奶茶加盟渠道权威推荐榜单:奶茶加盟/茶饮加盟/奶茶店加盟渠道精选
  • 2025优质小型环保腻子粉,植物腻子粉,净醛腻子粉,健康腻子粉,无味腻子粉,腻子粉厂家推荐,实用选型参考
  • 2025年河南心理健康咨询机构权威推荐:河南婚姻心理咨询/河南家庭心理咨询/河南心理咨询机构服务中心精选
  • 面试:安全框架与安全管理-网络-防火墙与IPS - 徐正柱
  • 2025年汽车机油,润滑油厂家推荐榜:聚焦能源高端化发展潜力!
  • 2025小众嵌入式一体机,悬臂式一体机,ARM一体机,一体机厂家推荐榜:朗宇智能,聚焦细分场景的实力之选
  • 2025西南地区小型花卉大棚,单栋大棚,玻璃温室大棚,温室大棚厂家实用推荐:青程农业,适配中小种植户需求
  • 2025 年 11 月 DALI 调光系统厂家推荐排行榜,调光网关/调光开关/调光电源/调光驱动/调光传感器/调光模块/调光控制系统公司推荐
  • 2025 年热电偶厂家最新推荐排行榜:聚焦 K 型 / T 型 / 铠装丝 / 高温热电偶优质品牌
  • 2025年今日黄金回收价格机构权威推荐榜单:黄金上门回收/回收黄金机构/现在黄金回收价格源头机构精选
  • 2025年汽车音响生产厂家权威推荐榜单:车载音响/汽车音响喇叭/汽车音响功放源头厂家精选
  • 问问
  • 2025年北京合同纠纷律师事务所权威推荐榜:专业律师团队与高效解决方案口碑之选
  • SQL - JOIN 中关联条件和过滤条件的执行顺序
  • 解析国标GB28181算法算力平台EasyGBS视频分发与按需直播关键技术,实现海量视频的高效触达
  • 解析国标GB28181算法算力平台EasyGBS视频分发与按需直播关键技术,实现海量视频的高效触达
  • KubernetesClient-C
  • CSP-S 2025 Self Review -- Words to be remembered 2025.11.3
  • 2025年微型减速机工厂权威推荐榜单:蜗轮蜗杆减速机/小齿减速机/谐波减速机源头厂家精选
  • 2025 年同步时钟厂家最新推荐榜,聚焦技术实力与市场口碑深度解析,涵盖卫星北斗 GPS 授时安全领域授时安全/授时防护/信号安全/时空安全同步时钟公司推荐
  • 关于combinational and sequential parts of an fsm described in same always block ,spyglass警告
  • 2025年云南好的旅行社公司权威推荐榜单:云南青年旅行社/云南正规的旅行社/云南省十大旅行社源头公司精选
  • 记录一次数据恢复,mysql8 - 义美
  • 2025年新能源水冷电机壳铝合金浇铸机批发厂家权威推荐榜单:户外围墙配件铝合金浇铸机/厨具锅铝合金浇铸机/手套模具铝合金浇铸机源头厂家精选