uni-app x开发商城系统,商品列表

news/2025/10/17 0:24:43/文章来源:https://www.cnblogs.com/xiao987334176/p/19145811

一、概述

上一篇已经实现了Icon 图标显示,接下来,展示商品列表数据,效果如下:

image

二、布局页面

可以看到,显示为2列,每一列有2条数据。

这里依然使用flex布局,在开发的时候,我们可以在页面中,先把数据固定好,可以随意创造一些数据。

 

index.uvue代码如下:

<template><view class="home"><!-- 轮播区域 --><up-swiper :list="swiper" keyName="img" indicator indicatorMode="dot" circular></up-swiper><!-- 导航区域 --><view class="nav"><view class="nav_item"><view class="iconfont icon-ziyuan"></view><text>网上超市</text></view><view class="nav_item"><view class="iconfont icon-guanyuwomen"></view><text>联系我们</text></view><view class="nav_item"><view class="iconfont icon-tupian"></view><text>社区图片</text></view><view class="nav_item"><view class="iconfont icon-shipin"></view><text>直播中心</text></view></view><!-- 推荐商品 --><view class="hot_goods"><view class="tit">推荐商品</view><view class="goods_list"><view class="goods_item"><image src="https://imgservice.suning.cn/uimg1/b2c/image/mMW59YHxKzNvTLlsdDlbVg.jpg_800w_800h_4e"mode=""></image><view class="price"><text>¥2195</text>cd<text>¥2499</text></view><view class="name">华为(HUAWVEI)荣耀6Plus 16G双4G版</view></view><view class="goods_item"><image src="https://2d.zol-img.com.cn/product/140_1200x900/735/cectVc8eFTXa.jpg" mode=""></image><view class="price"><text>¥5780</text><text>¥6388</text></view><view class="name">苹果Apple iPhone 6 Plus 16G 4G手机(联通三网版)</view></view></view></view></view>
</template><script>export default {data() {return {swiper: [{id: 1,url: 'https://picsum.photos',img: 'https://picsum.photos/600/400?random=1'}, {id: 2,url: 'https://picsum.photos',img: 'https://picsum.photos/600/400?random=2'}, {id: 3,url: 'https://picsum.photos',img: 'https://picsum.photos/600/400?random=3'},{id: 4,url: 'https://picsum.photos',img: 'https://picsum.photos/600/400?random=4'}],}},methods: {}}
</script><style lang="scss">.home {swiper {width: 750rpx;height: 380rpx;image: {height: 100%;width: 100%;}}.nav {display: flex;flex-direction: row; //横向排列justify-content: space-around; //平均分布在一行
.nav_item {text-align: center;view {width: 120rpx;height: 120rpx;background: $shop-color;border-radius: 60rpx;margin: 10px auto;line-height: 120rpx;color: white;font-size: 50rpx;text-align: center;}.icon-tupian {font-size: 45rpx;}text {font-size: 30rpx;}}}.hot_goods {background: #eee;overflow: hidden;margin-top: 10px;.tit {height: 50px;line-height: 50px;color: $shop-color;text-align: center;letter-spacing: 20px;background: #fff;margin: 7rpx 0;}.goods_list {padding: 0 15rpx;display: flex;flex-direction: row; //横向排列flex-wrap: wrap;justify-content: space-between;.goods_item {background: #fff;width: 355rpx;margin: 10rpx 0;padding: 15rpx;box-sizing: border-box;image {width: 80%;height: 150px;display: block;margin: auto; //图片居中
                    }.price {display: flex;flex-direction: row;color: $shop-color;font-size: 36rpx;// margin-top: 15rpx;margin: 20rpx 0 5rpx 0;text:nth-child(2) {color: #ccc;font-size: 28rpx;margin-top: 5rpx;margin-left: 17rpx;text-decoration-line: line-through;}}.name {font-size: 38rpx;line-height: 50rpx;padding-bottom: 15rpx;padding-top: 10rpx;}}}}}
</style>

 以上代码固定了2条数据,编译代码,效果如下:

image

 三、调用api获取商品数据

上面固定了数据,只是为了调整样式,真正使用,还是要通过api接口获取数据。

 

需要自己搭建一个服务端,用来提供api接口。这里为了节省时间,直接使用现有的。

链接: https://pan.baidu.com/s/1lPrvZrzPszxHzCgbEysIzg?pwd=6e8k 提取码: 6e8k

 

下载shop_server.zip,根据readme.md文件说明,进行部署即可。

最后访问2个接口,如果显示没问题,那么就可以正式调用API接口了。

 

修改uni-app x项目下的utils/request.ts,修改变量baseURL

config.baseURL = 'http://localhost:8082'  // api地址

 

修改index.uvue,改成api接口获取数据。

完整代码如下:

<template><view class="home"><!-- 轮播区域 --><up-swiper :list="swiper" keyName="img" indicator indicatorMode="dot" circular></up-swiper><!-- 导航区域 --><view class="nav"><view class="nav_item"><view class="iconfont icon-ziyuan"></view><text>网上超市</text></view><view class="nav_item"><view class="iconfont icon-guanyuwomen"></view><text>联系我们</text></view><view class="nav_item"><view class="iconfont icon-tupian"></view><text>社区图片</text></view><view class="nav_item"><view class="iconfont icon-shipin"></view><text>直播中心</text></view></view><!-- 推荐商品 --><view class="hot_goods"><view class="tit">推荐商品</view><view class="goods_list"><view class="goods_item" v-for="item in goods" :key="item.id"><image :src="item.img_url" mode=""></image><view class="price"><text>¥{{item.sell_price}}</text><text>¥{{item.market_price}}</text></view><view class="name">{{item.title}}</view></view></view></view></view>
</template><script>export default {data() {return {title: 'Hello',swiper: [],goods: []}},onLoad() {this.get_swiper()this.get_goods()},methods: {// 获取轮播图的数据
            async get_swiper() {try {const res = await this.$http.get('/api/getlunbo', {})// console.log("res", res)this.swiper = res.message} catch (err : any) {// console.error('获取轮播图失败', err)
                    uni.showToast({title: '获取轮播图失败' + err.statusCode,});}},// 获取热门商品列表数据
            async get_goods() {try {const res = await this.$http.get('/api/getgoods?pageindex=1', {})// console.log("res", res)this.goods = res.message} catch (err : any) {// console.error('获取轮播图失败', err)
                    uni.showToast({title: '获取热门商品列表失败' + err.statusCode,});}}}}
</script><style lang="scss">.home {swiper {width: 750rpx;height: 380rpx;image: {height: 100%;width: 100%;}}.nav {display: flex;flex-direction: row; //横向排列justify-content: space-around; //平均分布在一行
.nav_item {text-align: center;view {width: 120rpx;height: 120rpx;background: $shop-color;border-radius: 60rpx;margin: 10px auto;line-height: 120rpx;color: white;font-size: 50rpx;text-align: center;}.icon-tupian {font-size: 45rpx;}text {font-size: 30rpx;}}}.hot_goods {background: #eee;overflow: hidden;margin-top: 10px;.tit {height: 50px;line-height: 50px;color: $shop-color;text-align: center;letter-spacing: 20px;background: #fff;margin: 7rpx 0;}.goods_list {padding: 0 15rpx;display: flex;flex-direction: row; //横向排列flex-wrap: wrap;justify-content: space-between;.goods_item {background: #fff;width: 355rpx;margin: 10rpx 0;padding: 15rpx;box-sizing: border-box;image {width: 80%;height: 150px;display: block;margin: auto; //图片居中
                    }.price {display: flex;flex-direction: row;color: $shop-color;font-size: 36rpx;// margin-top: 15rpx;margin: 20rpx 0 5rpx 0;text:nth-child(2) {color: #ccc;font-size: 28rpx;margin-top: 5rpx;margin-left: 17rpx;text-decoration-line: line-through;}}.name {font-size: 38rpx;line-height: 50rpx;padding-bottom: 15rpx;padding-top: 10rpx;}}}}}
</style>

重新编译代码运行,整体效果如下:

动画

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/938588.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PySimpleGUI 中有没有类似VB的timer组件

在PySimpleGUI中,没有直接等同于VB的Timer组件,但可通过以下两种方式实现类似功能,且PySimpleGUI 5.0+版本提供了更优化的定时器管理方案: 一、通过window.read(timeout)模拟定时器原理:利用window.read(timeout)…

【填坑】电脑用户名有中文字符,如何与github建立SSH连接

前情提要 因为我之前很作死的在电脑本地用户名中设置了中文字符,导致始终无法与github建立ssh连接 如今通过hexo在github上建立了一个博客,为了更新方便,如今是必须要姐姐这个ssh连接github的历史遗留问题了。 好在…

向量空间与子空间

映射 对于集合 \(X,Y\),定义映射 \[F:X \to Y \]表示 \[\forall x\in X, F(x)\in Y \]若 \(\forall x_1\neq x_2\),\(F(x_1)\neq F(x_2)\),称 \(F\) 为单射。 若 \(\forall y\in Y\),\(\exists F(x)=y\),称 \(F\)…

西工大开源 Easy Turn:全双工轮次转换检测模型;百度 MuseSteamer 引入开放世界生成能力丨日报

开发者朋友们大家好:这里是 「RTE 开发者日报」 ,每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享 RTE(Real-Time Engagement) 领域内「有话题的技术」、「有亮点的产品」、「有思考的文章」、「有态…

10/16

今天体测了,肺活量5700,引体向上也有分,明天一定好好学习

MrakDown学习

MrakDown学习$(".postTitle2").removeClass("postTitle2").addClass("singleposttitle");MarkDown 标题 +空格+名字 字体 (粗体)Hello World (斜体)Hello World (斜体加粗)Hello World …

2025.10.16总结

对uml九种图的总结 1. 用例图核心描述:从用户(参与者)角度描述系统的功能需求。它定义了系统的边界,说明了“谁”在系统“内部”能“做什么”。 核心元素: 参与者:系统外部的、与系统交互的人、组织或其他系统。…

日常生活中的AI应用记录-2

2025-10-16 百度首页改版,加入了AI搜索功能,可以直接跳转到AI搜索页面. 稀土掘金首页-首要位置添加了-AICoding 栏目- infoq中-添加了多个AI相关的栏目- 51-CTO-中已经将人工智能板块,放在了网站的的首页位置, …

containerd二进制安装

containerd有两种安装包:第一种是containerd-xxx,这种包用于单机测试没问题,不包含runC,需要提前安装。第二种是cri-containerd-cni-xxx,baohan runC和k8s里所需要的相关文件。k8s集群需要用到此包。虽然包含runC,…

维修笔记 | 一例滤波电容老化引发开关电源异常现象

本文记录了一次开关电源故障的排查与维修过程。设备启动失败,经测试发现问题出在电源模块。拆解发现输出滤波电容鼓包,实际容值大幅下降。更换高频低阻电解电容后,输出恢复正常,设备运行稳定。结合容值测量与输出测…

(一)GPU与CUDA概述

前言GPU的发展起源可追溯至20世纪80-90年代,其核心驱动力来自游戏对浮点运算(FPU)的爆炸性需求。早期CPU的FPU性能仅为游戏需求的1/20,迫使英特尔通过MMX、SSE等向量处理单元提升并行计算能力,但仍无法满足需求。…

实验1 面向对象程序设计C++

实验任务1: 源代码task1.cpp1 #include<iostream>2 #include<string>3 #include<vector>4 #include<algorithm>5 6 template<typename T>7 void output(const T &c);8 9 void test…

练习篇:第一次markdown成果展示

学期2025-2024-1 学号20252332 《网络》第一周学习总结教材学习内容总结思维导图如下: 【金山文档- 网络空间安全导论学习总结】 (https://www.kdocs.cn/l/ciLWfzglzFzW) 教材学习中的问题和解决问题过程问题1:为什…

DirectX RayTracing (3) 程序图元及复杂光照

DirectX RayTracing (3) 程序图元及复杂光照 ​ 离上一篇文章隔的有点久了,在国庆前其实就看完了微软官方的案例并复刻了出来,但是一直懒得写,国庆也全拿去玩了,拖到过完了国庆才动笔。 ​ 在前面两篇中基本把 DX…

微软已停止对 Windows 10 系统的支持

参考https://www.microsoft.com/zh-cn/windows/end-of-support正文博 主 :夏秋初地 址 :https://www.cnblogs.com/xiaqiuchu/p/19146827 如果对你有帮助,可以点一下{$(#green_channel_follow).click()})() "…

NiN模型

NiN模型 import torch from torch import nn from d2l import torch as d2l def NiN_block(in_chanels,out_chanels,kernel_size,padding,stride):#NiN块return nn.Sequential(nn.Conv2d(in_chanels,out_chanels,kerne…

2025秋_13

今天学习了Java

2023 ICPC Hefei

2023 ICPC Hefei J 对于一条路径,维护最大的边权是容易的,但是要求路径上最大和次大的和。于是我们就枚举一条边来作为路径上的最大边权,然后取这条边的两个端点到原点和终点的路径上的最大边权为次大值就好了。只需…

斑马日记2025.10.16

今天学习了面向对象部分的静态成员变量和静态成员函数,其实也没有太大的收获,最近代码部分的状态和学习效率偏低,明天早上先学编程,再学英语; 今天看了一位大佬的直播,也有很多收获: 1.学历永远是最重要的; 2.…

可能是 ICPC2025 西安站游记

也可能不是赛前写游记有 buff,但无所谓了。 忘了哪天 发现可以去西安站,队伍名字沿用了 EC-Fianl。 队友进行了一些微调,从一个 NOI 得分比我高的换成了另一个 NOI 得分比我高的,怎么感觉全世界就我没去 NOI2025。…