第74讲Breadcrumb 面包屑实现

Breadcrumb 面包屑实现

为了实现二级路由,我们搞成搞个子路由,对于二级菜单

const routes = [
  {
   
    path: '/',
    name: '首页',
    component: () => import('../views/layout'),
    redirect:'/home',
    children:[
      {
   
        path: '/home',
        name: '首页',
        component: () => import('../views/home/index')
      },
      {
   
        path: '/bigType',
        name: '商品大类',
        component: () => import('../views/bigType/index')
      }
    ]
  },
  {
   
    path: '/login',
    name: 'login',
    component: () => import('../views/login')
  }
]

views/layout/header/breadcrumb.vue

<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item v-for="(item,index) in breadcrumbList" :key="index">
      <span class="no-redirect" v-if="index==breadcrumbList.length-1">{
   {
   item.name}}</span>
      <span class="redirect" v-else @click="handleRedirect(item.path)">{
   {
   item.name}}</span>
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script setup>
import {
   ref, watch} from 'vue'
import {
    useRoute,useRouter } from 'vue-router'
const route=useRoute();
const router=useRouter();
console.log(route.matched)

const breadcrumbList=ref([]);


const handleRedirect=(path)=>{
   
  router.push(path)
}

const initBreadcrumbList=()=>{
   
  breadcrumbList.value=route.matched;
}

watch(route,()=>{
   
  initBreadcrumbList();
},{
   deep:true,immediate:true})

</script>

<style lang="scss" scoped>
.no-redirect{
   
  cursor:text;
}
.redirect{
   
  color:#666;
  font-weight:600;
  cursor:pointer;
  &:hover{
   
    color:#304156
  }
}
</style>

layout index.vue修改:

<template>
  <div class="app-wrapper">
    <el-container>
      <el-aside width="200px" class="sidebar-container"><Menu/></el-aside>
      <el-container>
        <el-header>
          <div class="navbar">
            <Breadcrumb/>
          </div>
        </el-header>
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import Menu from '@/views/layout/menu'
import Breadcrumb from '@/views/layout/header/breadcrumb'
</script>

<style lang="scss" scoped>

.app-wrapper {
   
  position: relative;
  width: 100%;
  height: 100%;
}


.navbar {
   
  width: 100%;
  height: 60px;
  overflow: hidden;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0 16px;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  position: relative;
}

:deep(.el-header){
   
  padding: 0px;
}

.sidebar-container {
   
  background-color: #2d3a4b;
  height: 100%;
}

:deep(.el-container){
   
  height: 100%;
}

</style>

在这里插入图片描述
views/home/index.vue

<template>
  后台管理首页
</template>

<script>
export default {
   
  name: "index"
}
</script>

<style scoped>

</style>

views/bigType/index

<template>
商品大类
</template>

<script>
export default {
   
  name: "index"
}
</script>

<style scoped>

</style>

相关推荐

  1. vue2实现面包屑功能

    2024-02-11 17:00:02       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-11 17:00:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-11 17:00:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-11 17:00:02       82 阅读
  4. Python语言-面向对象

    2024-02-11 17:00:02       91 阅读

热门阅读

  1. django中自定义视图样式

    2024-02-11 17:00:02       52 阅读
  2. 计算机网络相关题目及答案(第七章)

    2024-02-11 17:00:02       48 阅读
  3. Shell - 学习笔记 - 2.10 - Shell字符串截取

    2024-02-11 17:00:02       52 阅读
  4. MySQL进阶查询篇(7)-触发器的创建和使用

    2024-02-11 17:00:02       56 阅读
  5. 【Rust日报】2024-02-10 扩展 Rust Effect 系统

    2024-02-11 17:00:02       49 阅读
  6. 计算机视觉主要知识点

    2024-02-11 17:00:02       46 阅读
  7. P6359 [CEOI2018] Cloud computing 题解

    2024-02-11 17:00:02       46 阅读
  8. 笔趣阁小说批量爬取脚本代码

    2024-02-11 17:00:02       53 阅读
  9. 906. 区间分组(贪心)

    2024-02-11 17:00:02       45 阅读
  10. GPT-4模型的创造力

    2024-02-11 17:00:02       49 阅读