效果如下:![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d60b497efd3389b647bb96a7b914f89d.png)
- 新建一个bread.vue组件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3b27a18534c4e57fe93f88fd9cbfda11.png)
- 在bread.vue组件代码如下:
<template>
<div class="bread">
<div class="example-container">
当前位置:
<el-breadcrumb separator=">">
<el-breadcrumb-item
v-for="(item, index) in breadList"
:key="index"
:to="{ path: item.path }"
>{{ item.meta.title }}</el-breadcrumb-item>
</el-breadcrumb>
</div>
</div>
</template>
<script>
export default {
data() {
return {
breadList: [],
};
},
watch: {
$route() {
this.getBreadcrumb();
},
},
methods: {
isHome(route) {
return route.name === "首页";
},
getBreadcrumb() {
let matched = this.$route.matched;
if (!this.isHome(matched[0])) {
matched = [{ path: "/", meta: { title: "首页" } }].concat(matched);
}
if (matched[matched.length - 1].meta.title == "企业业绩详情") {
matched = [
{ path: "/", meta: { title: "首页" } },
{ path: "/Results", meta: { title: "企业业绩" } },
].concat(matched[matched.length - 1]);
}
this.breadList = matched;
},
},
created() {
this.getBreadcrumb();
},
};
</script>
<style scoped>
.bread /deep/ .el-breadcrumb {
display: inline-block;
display: inline-block;
height: 48px;
vertical-align: top;
line-height: 48px;
}
.bread /deep/ .is-link {
font-weight: normal;
}
.bread {
float: left;
}
</style>
- 在路由文件中增加路由与属性:代码如下:
import Vue from 'vue'
import Router from 'vue-router'
import index from '@/components/index'
Vue.use(Router)
export default new Router({
linkActiveClass: 'active',
mode: '',
routes: [
{
path: '/',
name: 'index',
component: index,
meta: {
title: '首页'
}
},
{
path: '/Results',
name: 'Results',
component: () => import('@/components/results/Results'),
meta: {
title: '企业业绩',
},
},
{
path: '/results/ResultsDetail',
name: 'ResultsDetail',
component: () => import('@/components/results/ResultsDetail'),
meta: {
title: '企业业绩详情',
},
},
],
})
- 按需引入需要面包屑导航得组件、代码如下:
<template>
<div>
<bread></bread>
</div>
</template>
import bread from "@/components/bread";
export default {
name: "Results",
components: {
bread
}
}