Vue3+Elementplus引入面包屑功能
  FR8r9i3bpUMu 2023年11月02日 127 0

🍁 作者:知识浅谈,CSDN博客专家,阿里云签约博主,InfoQ签约博主,华为云云享专家,51CTO明日之星

📌 擅长领域:全栈工程师、爬虫、ACM算法

💒 公众号:知识浅谈

🔥 网站:vip.zsqt.cc

🤞Vue3+Elementplus引入面包屑功能总结🤞

Vue3+Elementplus引入面包屑功能_获取路径

正菜来了⛳⛳⛳

🎈路由内的内容

因为面包屑是根据路由的内容来显示的

{
    path: "/home",
    name: "home",
    // 懒加载
    component: () => import("../views/home/index.vue"),
    meta: {
      title: "主页",
    },
    children: [
    {
	    path: "/recruitManage",
	    name: "recruitManage",
	    component: () => import("../views/home/childrens/RecruitManage.vue"),
	    meta: {
	      title: "招聘管理",
	      icon: Guide
	    },
	    children: [
	      {
	        path: "/noticeList",
	        name: "noticeList",
	        // 懒加载
	        component: () => import("../views/home/childrens/NoticeList.vue"),
	        meta: {
	          title: "公告管理"
	        },
	      },
	      {
	        path: "/postList",
	        name: "postList",
	        // 懒加载
	        component: () => import("../views/home/childrens/PostList.vue"),
	        meta: {
	          title: "职位管理",
	        },
	      },
	    ]
  	}
 }

开始插入面包屑🎈

温馨提醒:这个有点仔细,请仔细看下去

<!-- 面包屑(放到你想要放的template中的位置) -->
<el-breadcrumb separator=">">
     <!-- <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item> -->
     <template v-for="(item, index) in breadList">
       <el-breadcrumb-item
         v-if="item.name"
         :key="index"
         :to="item.path"
       >{{ item.meta.title }}</el-breadcrumb-item>
     </template>
   </el-breadcrumb>
<script setup>
import { useRouter,useRoute } from 'vue-router';

let router = useRouter();
let route = useRoute();

let getMatched=()=>{
  console.log(route.matched);
  breadList.value = route.matched.filter(item => item.meta && item.meta.title);
}
onMounted(()=>{
  getMatched();
})

watch(() => route.path, (newValue, oldValue) => { //监听路由路径是否发生变化,之后更改面包屑
  breadList.value = route.matched.filter(item => item.meta && item.meta.title);
})

</script>

🍮插入内容讲解

📐第 1 步:导入route,使用其能访问到路由的路径

import { useRouter,useRoute } from 'vue-router';

let router = useRouter();
let route = useRoute();

📐第 2 步 :编写获取路径的方法 matched获取访问网址在路由中的路径,并过滤掉item没有title的元素,因为需要用title填充面包屑的内容

let getMatched=()=>{
  console.log(route.matched); //打印路径数组
  breadList.value = route.matched.filter(item => item.meta && item.meta.title);
}

📐第 3 步:页面加载时调用获取路径形成面包屑

onMounted(()=>{
  getMatched();
})

📐第 4 步 :监听路由发生变化面包屑进行相应的修改

watch(() => route.path, (newValue, oldValue) => { //监听路由路径是否发生变化,之后更改面包屑
  breadList.value = route.matched.filter(item => item.meta && item.meta.title);
})

🍚总结

以上就是面包屑在vue3和elementplus项目中的应用。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
FR8r9i3bpUMu