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

vue3 之 商城项目—一级分类

整体认识和路由配置

在这里插入图片描述
场景:点击哪个分类跳转到对应的路由页面,路由传对应的参数

router/index.js

import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
const router = createRouter({history: createWebHashHistory(import.meta.env.BASE_URL),routes: [{path: '/',name: 'layout',component: Layout,children: [{path: '',name: 'home',component: Home},{path: 'category/:id',name: 'category',component: Category}]},{path: '/login',name: 'login',component: Login},]
})export default router

layoutHeader.vue

<li v-for="item in categoryStore.categoryList" :key="item.id"><RouterLink active-class="active" :to="`/category/${item.id}`">{{ item.name }}</RouterLink>
</li>

面包屑导航渲染

在这里插入图片描述
在这里插入图片描述
封装接口

import request from '@/utils/request'/*** @description: 获取分类数据* @param {*} id 分类id * @return {*}*/
export const getTopCategoryAPI = (id) => {return request({url:'/category',params:{id}})
}

渲染面包屑导航

<script setup>import { findTopCategoryAPI } from '@/apis/category'const categoryData = ref({})const route = useRoute()const getCategory = async (id) => {// 如何在setup中获取路由参数 useRoute() -> route 等价于this.$routeconst res = await findTopCategoryAPI(id)categoryData.value = res.result}getCategory(route.params.id)
</script><template><div class="bread-container"><el-breadcrumb separator=">"><el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item><el-breadcrumb-item>{{ categoryData.name }}</el-breadcrumb-item></el-breadcrumb></div>
</template>

banner轮播图实现

在这里插入图片描述
在这里插入图片描述
适配接口

export function getBannerAPI (params = {}) {// 默认为1 商品为2const { distributionSite = '1' } = paramsreturn httpInstance({url: '/home/banner',params: {distributionSite}})
}

迁移首页Banner逻辑

<script setup>
// 部分代码省略
import { getBannerAPI } from '@/apis/home'// 获取banner
const bannerList = ref([])const getBanner = async () => {const res = await getBannerAPI({distributionSite: '2'})console.log(res)bannerList.value = res.result
}onMounted(() => getBanner())</script><template><div class="top-category"><div class="container m-top-20"><!-- 面包屑 --><div class="bread-container"><el-breadcrumb separator=">"><el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item><el-breadcrumb-item>{{ categoryData.name }}</el-breadcrumb-item></el-breadcrumb></div><!-- 轮播图 --><div class="home-banner"><el-carousel height="500px"><el-carousel-item v-for="item in bannerList" :key="item.id"><img :src="item.imgUrl" alt=""></el-carousel-item></el-carousel></div></div></div>
</template><style scoped lang="scss">
// 部分代码省略
.home-banner {width: 1240px;height: 500px;margin: 0 auto;img {width: 100%;height: 500px;}
}
</style>

激活状态显示和分类列表渲染

在这里插入图片描述
** 导航激活状态设置**

<RouterLink active-class="active" :to="`/category/${item.id}`">{{ item.name }}</RouterLink>

分类数据模版

在这里插入图片描述

<script setup>import GoodsItem from '../Home/components/GoodsItem.vue'
import { useBanner } from './composables/useBanner'
import { useCategory } from './composables/useCategory'
const { bannerList } = useBanner()
const { categoryData } = useCategory()</script><template><div class="top-category"><div class="container m-top-20"><!-- 面包屑 --><div class="bread-container"><el-breadcrumb separator=">"><el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item><el-breadcrumb-item>{{ categoryData.name }}</el-breadcrumb-item></el-breadcrumb></div><!-- 轮播图 --><div class="home-banner"><el-carousel height="500px"><el-carousel-item v-for="item in bannerList" :key="item.id"><img :src="item.imgUrl" alt=""></el-carousel-item></el-carousel></div><div class="sub-list"><h3>全部分类</h3><ul><li v-for="i in categoryData.children" :key="i.id"><RouterLink :to="`/category/sub/${i.id}`"><img :src="i.picture" /><p>{{ i.name }}</p></RouterLink></li></ul></div><div class="ref-goods" v-for="item in categoryData.children" :key="item.id"><div class="head"><h3>- {{ item.name }}-</h3></div><div class="body"><GoodsItem v-for="good in item.goods" :goods="good" :key="good.id" /></div></div></div></div>
</template><style scoped lang="scss">
.top-category {h3 {font-size: 28px;color: #666;font-weight: normal;text-align: center;line-height: 100px;}.sub-list {margin-top: 20px;background-color: #fff;ul {display: flex;padding: 0 32px;flex-wrap: wrap;li {width: 168px;height: 160px;a {text-align: center;display: block;font-size: 16px;img {width: 100px;height: 100px;}p {line-height: 40px;}&:hover {color: $xtxColor;}}}}}.ref-goods {background-color: #fff;margin-top: 20px;position: relative;.head {.xtx-more {position: absolute;top: 20px;right: 20px;}.tag {text-align: center;color: #999;font-size: 20px;position: relative;top: -20px;}}.body {display: flex;justify-content: space-around;padding: 0 40px 30px;}}.bread-container {padding: 25px 0;}
}.home-banner {width: 1240px;height: 500px;margin: 0 auto;img {width: 100%;height: 500px;}
}
</style>

解决路由缓存问题

什么是路由缓存问题
缓存问题:当路由path一样,参数不同的时候会选择直接复用路由对应的组件,相同的组件实例会被重复使用,两个路由渲染同一个组件,意味着组件的生命周期钩子不会被调用

问题:一级分类的切换正好满足上面条件,组件实例复用,导致分类数据无法更新

解决思路
1️⃣组件实例不复用,强制销毁重建
2️⃣监听路由变化,变化之后执行数据更新操作

解决方案

  1. 给 routerv-view 添加key属性,破坏缓存
<RouterView :key="$route.fullPath" />

在这里插入图片描述

  1. 使用 onBeforeRouteUpdate钩子函数,做精确更新
    在这里插入图片描述
// 封装分类数据业务相关代码
import { onMounted, ref } from 'vue'
import { getCategoryAPI } from '@/apis/category'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'export function useCategory () {// 获取分类数据const categoryData = ref({})const route = useRoute()const getCategory = async (id = route.params.id) => {const res = await getCategoryAPI(id)categoryData.value = res.result}onMounted(() => getCategory())// 目标:路由参数变化的时候 可以把分类数据接口重新发送onBeforeRouteUpdate((to) => {// 存在问题:使用最新的路由参数请求最新的分类数据getCategory(to.params.id)})return {categoryData}
}

总结
1️⃣路由参数问题产生的原因是什么?
路由只有参数变化时,会复用组件实例

2️⃣两种方案都可以解决路由缓存问题,如何选择
在意性能问题,选择onBeforeRouteUpdate,精细化控制
不在意性能问题,选择key,简单粗暴

使用逻辑函数拆分业务

概念理解
基于逻辑函数拆分业务是指把同一个组件中独立的业务代码通过函数做封装处理,提升代码的可维护性
在这里插入图片描述
实现步骤
1️⃣按照业务生命以 use 打头的逻辑函数
2️⃣把独立的业务逻辑封装到各个函数内部
3️⃣函数内部把组件中需要用到的数据或者方法return出去
4️⃣在组件中调用函数把数据或者方法组合回来使用

封装banner轮播图相关的业务代码

import { ref, onMounted } from 'vue'
import { getBannerAPI } from '@/apis/home'export function useBanner () {const bannerList = ref([])const getBanner = async () => {const res = await getBannerAPI({distributionSite: '2'})console.log(res)bannerList.value = res.result}onMounted(() => getBanner())return {bannerList}
}

组件内使用

import { useBanner } from './composables/useBanner'
const { bannerList } = useBanner()

核心总结
在这里插入图片描述

相关文章:

  • Python编写远程控制工具--被控端的编写
  • 哈工大团队顶刊发布!由单偏心电机驱动的爬行机器人实现多方向运动传递
  • DDoS攻击激增,分享高效可靠的DDoS防御方案
  • 使用Arcgis裁剪
  • HttpClient | 支持 HTTP 协议的客户端编程工具包
  • StringJoiner Sql拼接利器
  • Gradle IDEA 乱码
  • linux 下 chrome 无法在设置里面配置代理的解决方法
  • 标准库 STM32+EC11编码器+I2C ssd1306多级菜单例程
  • 【JAVA WEB】盒模型
  • OpenEuler20.03LTS SP2 上安装 OpenGauss3.0.0 单机部署过程(二)
  • Webpack插件浅析
  • 4.0 Zookeeper Java 客户端搭建
  • 高仿原神官网UI 纯html源码
  • SpringBoot日志插件log4J和slf4J的使用和比较含完整示例
  • Android Volley源码解析
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • Apache的基本使用
  • Brief introduction of how to 'Call, Apply and Bind'
  • extract-text-webpack-plugin用法
  • in typeof instanceof ===这些运算符有什么作用
  • Iterator 和 for...of 循环
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • java取消线程实例
  • JS实现简单的MVC模式开发小游戏
  • js数组之filter
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • 编写高质量JavaScript代码之并发
  • 产品三维模型在线预览
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 为视图添加丝滑的水波纹
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • 容器镜像
  • ​flutter 代码混淆
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • #pragma pack(1)
  • (13)Hive调优——动态分区导致的小文件问题
  • (C#)获取字符编码的类
  • (LeetCode) T14. Longest Common Prefix
  • (rabbitmq的高级特性)消息可靠性
  • (Redis使用系列) Springboot 使用Redis+Session实现Session共享 ,简单的单点登录 五
  • (附源码)计算机毕业设计ssm电影分享网站
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (十一)c52学习之旅-动态数码管
  • (转)负载均衡,回话保持,cookie
  • .axf 转化 .bin文件 的方法
  • .cfg\.dat\.mak(持续补充)
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET Core跨平台微服务学习资源
  • .NET 使用配置文件
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .NET/C# 使窗口永不获得焦点