vue+elementUI动态生成面包屑导航教程

作者:sun shying 时间:2024-05-02 17:12:26 

效果如下所示:

vue+elementUI动态生成面包屑导航教程

项目需要动态生成面包屑导航,并且首页可以点击.其余为路径显示


<el-menu :unique-opened="true" router :default-active="$route.path" @select="handleSelect">
   <div class="user-menu-box" v-for="menu in menus" :key="menu.id">
     <el-menu-item v-if="!menu.child" :index="menu.path">
       <icon :name="menu.icon" :w="20" :h="20"></icon>
       <span slot="title" v-text="menu.name"></span>
     </el-menu-item>
     <el-submenu v-if="menu.child" :index="menu.path">
       <template slot="title">
         <icon :name="menu.icon" :w="20" :h="20"></icon>
         <span slot="title" v-text="menu.name"></span>
       </template>
       <el-menu-item-group>
         <el-menu-item v-for="subMenu in menu.child" :key="subMenu.id" v-text="subMenu.name"
           :index="subMenu.path"></el-menu-item>
       </el-menu-item-group>
     </el-submenu>
   </div>
</el-menu>

上面的代码是elementUI组件的样式,根据项目需要做了修改,搬运的时候根据项目自己改改


export default {
 data () {
   return {
     activeMenu: '',
     indexBreadcrumbs: [],
     menus: [{
       id: '1',
       name: '门户管理',
       icon: 'menhuguanli',
       path: '#2',
       child: [{
         id: '1-1',
         name: '轮播图',
         path: '/backstage/protaManage/turns'
       }, {
         id: '1-2',
         name: '基础数据',
         path: '/backstage/protaManage/basis'
       }, {
         id: '1-3',
         name: '分类管理',
         path: '/backstage/protaManage/classify'
       }, {
         id: '1-4',
         name: '内容发布',
         path: '/backstage/protaManage/content'
       }]
     }, {
       id: '2',
       name: '我的云盘',
       icon: 'yunpan',
       path: '/backstage/yunManage'
     }, {
       id: '3',
       name: '管理菜单',
       icon: 'shezhi',
       path: '/backstage/menusManage'
     }, {
       id: '4',
       name: '编辑板块',
       icon: 'fabuzhongxin',
       path: '/backstage/editPlate'
     }]
   }
 },
 watch: {
   $route () {
     this.handChange()
   }
 },
 computed: {
   breadcrumbList () {
     let breadcrumbs = []
     let menuList = this.menus
     this.indexBreadcrumbs.map(item => {
       for (let i = 0; i < menuList.length; i++) {
         if (item === menuList[i].path) {
           breadcrumbs.push(menuList[i])
           if (menuList[i].child) {
             menuList = menuList[i].child
           }
           break;
         }
       }
     })
     return breadcrumbs
   }
 },
 methods: {
   handChange () {
     this.$emit('hand-change', this.breadcrumbList)
   },
   handleSelect (index, indexPath) {
     this.indexBreadcrumbs = indexPath
   }
 },
 created () {
   this.handChange()
 }
}

上面的代码是模拟的数据,调用elememtUI的组件导航菜单的中的@select方法,有两个参数,可以自行打印

vue+elementUI动态生成面包屑导航教程

然后在computed中计算当前选中的是哪一部分,取到返回值,然后$emit传给父组件,


<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/backstage' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item v-for="o in breadcrumbList" :key="o.id">{{o.name}}
  </el-breadcrumb-item>
</el-breadcrumb>

父组件中取到子组件传过来的值后,直接渲染就行了

来源:https://blog.csdn.net/ju__ju/article/details/101513973

标签:vue,elementUI,面包屑,导航
0
投稿

猜你喜欢

  • Python使用matplotlib 画矩形的三种方式分析

    2022-04-06 05:49:17
  • python有证书的加密解密实现方法

    2023-02-10 08:07:30
  • lambda 表达式导致 Arthas 无法 redefine 的问题

    2023-08-25 18:05:05
  • python使用Apriori算法进行关联性解析

    2022-08-15 13:02:10
  • 手动实现vue2.0的双向数据绑定原理详解

    2024-04-27 16:09:15
  • Pyqt QImage 与 np array 转换方法

    2022-01-03 01:18:53
  • 用sleep间隔进行python反爬虫的实例讲解

    2023-02-10 07:00:42
  • OpenCV-PS扩散毛玻璃效果的实现代码

    2022-03-17 22:45:52
  • 详细解读Python字符串的使用与f-string

    2023-06-29 07:24:21
  • Pycharm如何自动生成头文件注释

    2022-04-11 17:31:42
  • Sql Server 视图数据的增删改查教程

    2024-01-22 07:51:05
  • SQLSERVER ISNULL 函数与判断值是否为空的sql语句

    2024-01-13 02:58:14
  • Win10用vscode打开anaconda环境中的python出错问题的解决

    2023-04-21 09:21:21
  • [翻译]标记语言和样式手册 Chapter 16 下一步

    2008-02-22 17:47:00
  • python语言中pandas字符串分割str.split()函数

    2022-01-30 16:55:56
  • python 爬取英雄联盟皮肤图片

    2021-08-10 10:16:06
  • FCKeditor ASP.NET 上传附件研究

    2023-12-30 05:27:22
  • 解决MySql版本问题sql_mode=only_full_group_by

    2024-01-14 00:23:17
  • 给SQL Server传送数组参数的变通办法

    2008-11-25 11:39:00
  • python自制包并用pip免提交到pypi仅安装到本机【推荐】

    2023-12-14 19:33:23
  • asp之家 网络编程 m.aspxhome.com