整体业务认识和路由配置

创建/views/Subcategory/index.vue文件

1. 准备组件模版

<script setup>
</script>

<template>
  <div class="container ">
    <!-- 面包屑 -->
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/' }">居家
        </el-breadcrumb-item>
        <el-breadcrumb-item>居家生活用品</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <el-tabs>
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
      <div class="body">
         <!-- 商品列表-->
      </div>
    </div>
  </div>

</template>

<style lang="scss" scoped>
.bread-container {
  padding: 25px 0;
  color: #666;
}

.sub-container {
  padding: 20px 10px;
  background-color: #fff;

  .body {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
  }

  .goods-item {
    display: block;
    width: 220px;
    margin-right: 20px;
    padding: 20px 30px;
    text-align: center;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>

2. 配置路由关系

{
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          component: Home
        },
        {
          path: 'category/:id',
          component: Category
        },
				// 配置这里
        {
          path: 'category/sub/:id',
          name: 'subCategory',
          component: SubCategory
        },
      ]
    },

3. 跳转配置

一定要配置路由跳转

<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>

面包屑导航实现

1. 获取数据

注意保持变量名称一致,名字大小写也要一致

<script setup>
import { onMounted, ref } from "vue";
import { getCategoryFilterAPI } from "@/apis/category"
import { useRoute } from "vue-router";

const FilterData = ref({})
const route = useRoute();
const getFilterData = async() => {
  const res = await getCategoryFilterAPI(route.params.id)
  FilterData.value = res.result;
  console.log(FilterData)
}
onMounted(()=>getFilterData())
</script>

2. 在导航栏渲染

<!-- 面包屑 -->
<div class="bread-container">
  <el-breadcrumb separator=">">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: `/category/${FilterData.parentId}` }">{{ FilterData.parentName }}
    </el-breadcrumb-item>
    <el-breadcrumb-item>{{FilterData.name}}</el-breadcrumb-item>
  </el-breadcrumb>
</div>

分类基础列表实现

1. 准备接口

category.js

export const getSubCategoryAPI = (data) => {
  return httpInstance({
    url:'/category/goods/temporary',
    method:'POST',
    data
  })
}

2. 获取数据