vue-cli项目中—根据路由变换的动态面包屑导航
1、在src文件夹下的router/index.js中配置路由
const routerMap = [
{
path: '/',
redirect: 'dashboard',
component: Layout,
name:'Dashboard',
children: [
{
path: 'dashboard',
component: () => import('@/view/dashboard'),
name: 'Dashboard',
meta: { title: 'dashboard', icon: 'dashboard', noCache: true }
}
]
},{
path:'/test',
component:Layout,
redirect: '/test/index',
name:'Test',
meta:{title:'test',icon:'lock'},
children:[
{
path:'index',
component:()=>import('@/view/test'),
name:'test',
meta:{title:'test', icon: 'example', noCache: true }
},{
path:'example',
component:()=>import('@/view/test/example'),
name:'example',
meta:{title:'example',icon:'404'}
}
]
}
]
2、面包屑组件封装
<template>
<div class="navbar clearfix">
<el-breadcrumb class="breadcrumb-container" separator-class="el-icon-arrow-right">
<el-breadcrumb-item v-for="item in levelList":key="item.path" :to="item.path">{{item.meta.title}}</el-breadcrumb-item>
</el-breadcrumb>
</div>
</template>
<script>
export default {
name: 'Navbar',
data() {
return {
levelList: []
}
},
watch: {
$route() {
this.getBreadcrumb()
}
},
created(){
this.getBreadcrumb()
},
methods:{
getBreadcrumb() {
//$route.matched一个数组 包含当前路由的所有嵌套路径片段的路由记录
let matched = this.$route.matched.filter(item => item.name)
const first = matched[0];
if (first && first.name.trim().toLocaleLowerCase() !== 'Dashboard'.toLocaleLowerCase()) {
matched = [{ path: '/dashboard', meta: { title: 'dashboard' }}].concat(matched)
}
this.levelList = matched
}
}
}
</script>
3、页面中使用面包屑导航组件
<template>
<div>
<!-- 用户组--管理 -->
<breadcrumb></breadcrumb>
</div>
</template>
<script>
// 引入【面包屑导航】组件
import Breadcrumb from '../../components/breadcrumb/index'
export default {
components:{
Breadcrumb
}
}
</script>