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

六、实战开发 uni-app x 项目(仿京东)- 分类页

书接上回,我们来设计分类页,右键左侧项目管理器中的pages,在右键菜单选择新建页面

根据下面的图,设置相关内容

在左侧项目管理中,就会看到多出了category目录,目录下多出了category.uvue文件

修改category.uuvue文件

<template>
	<view class="category">
		<!-- 一级分类 -->
		<scroll-view class="category-level1" scroll-y>
			<view class="category-level1-item" v-for="(item, index) in categoryLevel1List" :key="index"
				:class="{ active: activeLevel1Index === index }" @click="handleLevel1Click(index)">
				<text>{
  { item.name }}</text>
			</view>
		</scroll-view>

		<!-- 二级分类 + 商品列表 -->
		<view class="category-content">
			<!-- 二级分类 -->
			<view class="category-level2">
				<view class="category-level2-item" v-for="(item, index) in categoryLevel2List" :key="index"
					:class="{ active: activeLevel2Index === index }" @click="handleLevel2Click(index)">
					<text>{
  { item.name }}</text>
				</view>
			</view>

			<!-- 商品列表 -->
			<view class="goods-list">
				<view class="goods-item" v-for="(item, index) in goodsList" :key="index">
					<image :src="item.imageUrl" mode="aspectFill" />
					<text class="goods-item-name">{
  { item.name }}</text>
					<text class="goods-item-price">¥{
  { item.price }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 一级分类数据
				categoryLevel1List: [
					{ id: 1, name: '手机' },
					{ id: 2, name: '电脑' },
					{ id: 3, name: '家电' },
					{ id: 4, name: '服饰' }
				],
				// 当前选中一级分类索引
				activeLevel1Index: 0,
				// 二级分类数据
				categoryLevel2List: [],
				// 当前选中二级分类索引
				activeLevel2Index: 0,
				// 商品列表数据
				goodsList: []
			};
		},
		methods: {
			// 一级分类点击事件
			handleLevel1Click(index) {
				this.activeLevel1Index = index;
				// 根据一级分类 id 获取二级分类数据
				this.getCategoryLevel2List(this.categoryLevel1List[index].id);
			},
			// 二级分类点击事件
			handleLevel2Click(index) {
				this.activeLevel2Index = index;
				// 根据二级分类 id 获取商品列表数据
				this.getGoodsList(this.categoryLevel2List[index].id);
			},
			// 模拟获取二级分类数据
			async getCategoryLevel2List(level1Id) {
				// 模拟接口请求
				const mockData = {
					1: [
						{ id: 11, name: '智能手机' },
						{ id: 12, name: '老人手机' },
						{ id: 13, name: '游戏手机' },
					],
					2: [
						{ id: 21, name: '笔记本' },
						{ id: 22, name: '台式机' },
						{ id: 23, name: '平板电脑' }
					],
					3: [
						{ id: 31, name: '冰箱' },
						{ id: 32, name: '洗衣机' },
						{ id: 33, name: '空调' }
					],
					4: [
						{ id: 41, name: '男装' },
						{ id: 42, name: '女装' },
						{ id: 43, name: '童装' }
					]
				};
				this.categoryLevel2List = mockData[level1Id] || [];
				// 默认选中第一个二级分类
				this.handleLevel2Click(0);
			},
			// 模拟获取商品列表数据
			async getGoodsList(level2Id) {
				// 模拟接口请求
				const mockData = {
					11: [
						{ id: 111, name: 'iPhone 14', price: 5999, imageUrl: '
http://www.dtcms.com/a/73259.html

相关文章:

  • 【二分算法】-- 寻找旋转排序数组中的最小值
  • 2025 香港 Web3 嘉年华:全球 Web3 生态的年度盛会
  • 如何进行前端项目的自动化部署?请简述主要流程和常用工具。
  • 电子电气架构 --- 智能座舱和车载基础软件简介
  • Qt 窗口以及菜单栏介绍
  • 谷歌搜索基本规则
  • 算法014——找到字符串中所有字母异位词
  • C++|构造函数和析构函数
  • 基于PHP的网店进销存管理系统(源码+lw+部署文档+讲解),源码可白嫖!
  • 练习-依依的询问最小值(前缀和差分)
  • C语言之链表
  • NocoBase 本周更新汇总:双因素身份认证(2FA)
  • HTML 字符集
  • Kubernetes的组成和架构
  • 一个简单的 **猜数字游戏** 的 C 语言例程
  • Java 用循环嵌套语句生成*三角形阵列
  • Vue3组件+leaflet,实现重叠marker的Popup切换显示
  • 51单片机的工作过程
  • Vue-Router:构建单页面应用的路由管理利器
  • 向量数据库技术系列四-FAISS介绍
  • three.js解决非全屏射线位置错误问题
  • 第三章:大模型中的提示学习
  • 蓝桥杯备考:图论之Prim算法
  • 2.3 滑动窗口专题:最大连续1的个数 III(LeetCode 1004)
  • PostgreSQL技术大讲堂 - 第82讲,主题:数据安全利器--密码安全策略构建
  • SpringBoot 和vue前后端配合开发网页拼图10关游戏源码技术分享
  • c++图论(一)之图论的起源和图的概念
  • 易语言模拟真人鼠标轨迹算法
  • 2025年渗透测试面试题总结-某四字大厂实习面试复盘 一面 三面(题目+回答)
  • Amazon Quantum Ledger Database (QLDB):革新数据可信记录的终极解决方案