后台管理---面包蟹组件封装

1.因为这个面包屑在其他项目中可能也会用到,所以将其放在公共文件夹中

 2.给这个组件设置一些属性

3.类型接口定义

export interface IBreadcrumb {
  name: string
  path?: string
}

4.在视图接收这些数据

 5.在父组件进行使用

# 当前面包蟹是一个空数组,怎么获取当前面包屑的数组

1.首先我们可以使用这个工具函数:根据路径currentPath 路径匹配到最里面的菜单(menu);

  就是type===2 的一个菜单,还需要拿到上一层type===1 时候的menu

//面包蟹工具
export function pathMapBreadcrumbs(userMenus: any[], currentPath: string):any{ const breadcrumbs:IBreadcrumb[]=[]
for (const menu of userMenus) {
 if (menu.type === 1) {
   //递归查找
      const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
      // 找到就返回
      if (findMenu) {
     breadcrumbs.push({name:menu.name,path:menu.url})
     breadcrumbs.push({name:findMenu.name,path:findMenu.url})
     return findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      return menu
    }
  }
 return breadcrumbs   //这样就可以拿到我们的breadcrumbs了
}

 

#这个文件夹里面的重复代码太多

1.可以合并为一个函数

export function pathMapBreadcrumbs(userMenus: any[], currentPath: string):any{
  const breadcrumbs:IBreadcrumb[]=[]
pathMapToMenu(userMenus,currentPath,breadcrumbs) //直接调用就可以了
 return breadcrumbs   //这样就可以拿到我们的breadcrumbs了
}

export function pathMapToMenu(
  userMenus: any[], 
  currentPath: string,
  breadcrumbs?:IBreadcrumb[]
  ): any {  //根据路径currrentPath路径匹配到最里面的菜单(menu)的:就是type 等于2 的一个菜单,还需要拿到上一层type===1 的时候的menu
  //对菜单做一个遍历,
  for (const menu of userMenus) {
    if (menu.type === 1) {
      //递归查找
      const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
      // 找到就返回
      if (findMenu) {
        breadcrumbs?.push({name:menu.name,path:menu.url})
        breadcrumbs?.push({name:findMenu.name,path:findMenu.url})
        return findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      return menu
    }
  }
}
export {firstMenu}

 

 #在页面使用

1.代码展示

 最终效果展示:

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值