静态结构搭建和路由配置

1. 准备分类组件

创建views/Category/index.vue组件

<script setup>

</script>

<template>
  <div class='top-category'>
    我是分类
  </div>
</template>

2. 配置路由

path: '/',
    component: Layout,
    children: [
      {
        path: '',
        component: Home
      },
      {
        // 修改这里
        path: 'category/:id',
        component: Category
      }
    ]
  },

3. 配置导航区域链接

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

面包屑导航渲染

1. 认识组件准备模版

修改views/Category.index.vue

<script setup>

</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>居家</el-breadcrumb-item>
        </el-breadcrumb>
      </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;
  }
}
</style>

2. 封装接口

获取分类数据

import httpInstance from '@/utils/http'

export const getTopCategoryAPI = (id) => {
  return httpInstance({
    url:'/category',
    params:{
      id
    }
  })
}

3. 渲染面包屑导航

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

const categoryData = ref({})
const route = useRoute()

const getCategory = async() => {
    // 获取路由参数,如果是:/id就用route.params.id
    const res = await getTopCategoryAPI(route.params.id)
    categoryData.value = res.result
}

onMounted(()=> getCategory())
</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>
  </div>
</template>

分类Banner渲染

1. 适配接口

apis/home.js