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

Element UI动态实现面包屑导航~

思路:监听路由变化,在路由规则中添加meta然后在组件中渲染。

import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter)
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {return originalPush.call(this, location).catch(err => err)
}const router = new VueRouter({routes:[{path:'/',redirect:'/welcome',},{path:'/home',name:'home',component:()=>import('../src/view/showIndex.vue'),meta:{title:'首页'},children:[{path:'/user',name:'user',component:()=>import('../src/view/user.vue'),meta:{title:'用户管理'}},{path:'/categroy',name:'categroy',component:()=>import('../src/view/categroy.vue'),meta:{title:'品类管理'}},{path:'/product',name:'product',component:()=>import('../src/view/product.vue'),meta:{title:'商品生产'}},{path:'/sex',name:'sex',component:()=>import('../src/view/sex.vue'),meta:{title:'角色管理'}},{path:'/welcome',name:'welcome',component:()=>import('../src/view/welcome.vue'),}]},{path:'/shang',name:'shang',component:()=>import('../src/view/showIndex.vue'),meta:{title:'商品'},children:[{path:'/user2',name:'user2',component:()=>import('../src/view/user2.vue'),meta:{title:'用户管理2'}},]},{path:'/login',name:'login',component:()=>import('../src/view/loginNews.vue')},
]
})
// 路由前置守卫
router.beforeEach((to,from,next) => {const token = localStorage.getItem("token")if(to.path ==='/login'){next()}else{if(token){next()}else{next('/login')}}})export default router
<template><div class="home"><!-- 首页 --><el-container><el-header><!-- 头部 --><showTop></showTop></el-header><el-container><el-aside width="200px;"><AsideLeft></AsideLeft></el-aside><el-main><!-- 面包屑导航 --><Breadcrumb></Breadcrumb><router-view></router-view></el-main></el-container></el-container></div>
</template><script>
import showTop from "../components/showTop.vue";
import AsideLeft from "../components/AsideLeft.vue";
import Breadcrumb from "../components/Breadcrumb.vue"
export default {name: "showIndex",components: {showTop,AsideLeft,Breadcrumb},
};
</script><style>
.el-header {background-color: #b3c0d1;color: #ffffff;text-align: center;line-height: 60px;background-color: rgb(60, 60, 60);
}.el-aside {background-color: rgb(60, 60, 60);color: #333;text-align: center;line-height: 200px;position: absolute;top: 60px;bottom: 0px;
}.el-main {background-color: #e9eef3;color: #333;text-align: center;line-height: 160px;position: absolute;top: 60px;left: 200px;right: 0;bottom: 0px;
}body > .el-container {margin-bottom: 40px;position: relative;
}
</style>

<template><!-- 面包屑导航 --><div><el-breadcrumb separator-class="el-icon-arrow-right"><el-breadcrumb-itemv-for="(item, index) in list":key="index":to="item.path">{{ item.title }}</el-breadcrumb-item></el-breadcrumb></div>
</template><script>
export default {name: "BreadCrumb",data() {return {list: [],};},mounted(){//   解决刷新面包屑消失问题if(localStorage.getItem('BreadCrumb')){this.list = JSON.parse(localStorage.getItem('BreadCrumb'))}},watch: {$route() {console.log("this.$route", this.$route);this.fn()},},methods: {fn() {this.list = [];const matched = this.$route.matched;matched.forEach((item) => {// 首页不需要面包屑导航if (this.$route.name == "welcome" || this.$route.name == "home") {return;}this.list.push({ title: item.meta.title, path: item.path });});//   解决刷新面包屑消失问题localStorage.setItem('BreadCrumb',JSON.stringify(this.list))},},
};
</script><style></style>

http://www.lryc.cn/news/423217.html

相关文章:

  • 安科瑞Acrel-2000ES储能能量管理系统在新型电力系统下分布式储能的研究
  • Git 逆转时光:版本回退操作详解
  • 8.6.数据库基础技术-数据库的控制
  • php语言基础入门
  • 告别杂音,从 AI 音频降噪开始
  • Postman中params传参与Body传参区别以及Body中不同类型的区别
  • 数据结构入门——05队列
  • 使用python MySQL 实现一个 key-value(string:json) 读写库
  • 实现:uniapp项目分享视频页面到微信,可以自定义分享的页面样式及内容
  • 【C++】—— 类与对象(五)
  • scp命令的使用
  • 定位和解决线上接口性能优化或者数据库性能优化的思路是什么?
  • 修改docker的/var/lib/docker/overlay2储存路径
  • 解决中国式报表难题,这款工具真的免费且好用
  • 图解Kafka | 彻底弄明白 Kafka 两个最重要的配置
  • 创建线程的三种方式
  • 官宣|Apache Flink 1.20 发布公告
  • HarmonyOS应用一之登录页面案例
  • KubeSphere 部署 Kafka 集群实战指南
  • 手把手教你安装音乐制作软件FL Studio 24.1.1.4285中文破解版
  • SDL 与 OpenGL 的关系
  • 考研数学 线性代数----行列式与矩阵
  • Golang实现简单的HTTP服务,响应RESTful请求判断形状大小
  • 使用C语言构建Lua库
  • 愤怒的江小白,这几年怎么了?
  • Unity教程(十)Tile Palette搭建平台关卡
  • 如何防止员工私自拷贝公司内部文件?安企神帮你解决泄密之忧
  • Linux应急响应检查工具
  • CP AUTOSAR标准之EEPROMAbstraction(AUTOSAR_SWS_EEPROMAbstraction)(更新中……)
  • 重启redis服务时报错:Failed to start redis.service: Unit not found