vue + element-UI 实现面包屑

1. 路由文件中配置

 // router.js
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

const Login = () => (import('@/components/Login'))
const Home = () => (import('@/components/Home/index'))

const router = new Router({
  routes: [
    {
      path: "/",
      name: "首页",
      redirect: '/home'
    },
{
      path: '/home',
      component: Home,
      meta: { requiresAuth: true },
      children: [
        {
          path: '/',
          redirect: './overview'
        },
        { path: '/home/overview', name: "系统总览", component: Overview, meta: { title: '系统总览' } },  // 配置meta
        { path: '/home/nodeTree/tree', name: "菜单管理", component: nodeTree, meta: { title: '菜单管理' } },  // 配置meta
        { path: '/home/water/all', name: "水厂监测", component: WaterallPage, meta: { title: '水厂监测' } }  // 配置meta
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    { path: '*', component: NotFoundComponent }
  ],
  linkActiveClass: "active-router",
  linkExactActiveClass: "exact-router"
})
export default router

2. 组件内容

<template>
      <el-breadcrumb class="app-breadcrumb" separator-class="el-icon-arrow-right">
          <el-breadcrumb-item v-for="(item)  in levelList" :key="item.path" v-if="item.meta.title">
            <router-link :to="item.redirect||item.path">{{item.meta.title}}</router-link>
          </el-breadcrumb-item>
      </el-breadcrumb>
</template>

<script>
export default {
  data() {
      return {
          levelList: null
      }
  },
  methods: {
          /**
           * 生成面包屑的方法
           */
    getBreadcrumb() {
      let matched = this.$route.matched.filter(item => item.name)
      const first = matched[0]
      if (first && first.name !== '首页') {
        matched = [{path: '/home', meta: { title: '首页' }}].concat(matched)
      }
      this.levelList = matched;
    }
  },
  mounted() {
    this.getBreadcrumb();
  },
  watch: {
    $route(to, from) {
      this.getBreadcrumb();
    }
  }
}
</script>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,183评论 19 139
  • 目录 一、项目结构的核心思想 二、项目目录结构 三、资源路径编译规则 四、index.html 五、build目录...
    科研者阅读 13,917评论 0 40
  • 唐糖在回美国之前,每天陪着爷爷奶奶吃饭,遛弯。 唐糖仿佛回到了16岁时,她又变成了那个活力四射爱打抱不平的幸福少女...
    Alice鲸鱼咩阅读 3,465评论 0 0
  • 要混币圈,总得了解一些基本知识,股神巴菲特说过,只投自己熟悉,了解的公司股票。所以要投资数字货币,就要有一些基本的...
    海升125阅读 2,726评论 0 0
  • 您碍不了我在长大 我碍不了您发的斑白 时光总扼杀我在您身边的时间
    不久不留阅读 1,522评论 0 0

友情链接更多精彩内容