menu.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <el-menu :default-active="active" v-bind="config" class="el-menu-vertical-demo" :collapse="isCollapse">
  3. <naf-menu-item @naf-menu-item="menuItem" v-for="(item, index) in menuItems" :key="index" :item="item" :index="item.path"></naf-menu-item>
  4. </el-menu>
  5. </template>
  6. <script>
  7. import nafMenuItem from './menu-item'
  8. const config = {
  9. backgroundColor: process.env.VUE_APP_MENU_BACKGROUNDCOLOR,
  10. textColor: process.env.VUE_APP_MENU_TEXTCOLOR,
  11. activeTextColor: process.env.VUE_APP_MENU_ACTIVETEXTCOLOR
  12. }
  13. export default {
  14. props: {
  15. // 菜单折叠
  16. isCollapse: { type: Boolean, default: false },
  17. // 树形结构菜单数据
  18. menuItems: Array
  19. },
  20. components: {
  21. nafMenuItem
  22. },
  23. computed: {
  24. // 按钮选中状态计算树形
  25. active () {
  26. // 如果是首页返回空
  27. if (this.$route.path === '/frame') return ''
  28. // 定义当前选中的菜单
  29. let active = ''
  30. // 自定义函数 参数是当前路由与需要过滤的数组
  31. const item = (path, items) => {
  32. // 数组过滤
  33. items.filter(p => {
  34. // 如果数组地址等于传入地址 给当前项赋值
  35. if (`/frame${p.path}` === path) active = p.path
  36. // 如果存在子级数组 递归调用
  37. if (p.children) item(path, p.children)
  38. })
  39. }
  40. // 当前路由
  41. const path = this.$route.path
  42. // 调用自定义函数
  43. item(path, this.menuItems)
  44. return active
  45. }
  46. },
  47. data () {
  48. return {
  49. config
  50. }
  51. },
  52. methods: {
  53. // 菜单点击跳转地址
  54. menuItem (e) {
  55. const url = `/frame${e.path}`
  56. if (url === this.$route.path) return
  57. this.$router.push(url)
  58. }
  59. },
  60. mounted () {}
  61. }
  62. </script>
  63. <style lang="less" scoped>
  64. .el-menu-vertical-demo {
  65. height: 100%;
  66. }
  67. </style>