sidebarItem.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <div class="menu-wrapper">
  3. <template v-for="item in menu">
  4. <el-menu-item
  5. v-if="validatenull(item[childrenKey]) && vaildRoles(item)"
  6. :index="item[pathKey]"
  7. @click="open(item)"
  8. :key="item[labelKey]"
  9. :class="{ 'is-active': vaildAvtive(item) }"
  10. >
  11. <i :class="item[iconKey]"></i>
  12. <span slot="title" :alt="item[pathKey]">{{
  13. generateTitleLab(item)
  14. }}</span>
  15. </el-menu-item>
  16. <el-submenu
  17. v-else-if="!validatenull(item[childrenKey]) && vaildRoles(item)"
  18. :index="item[pathKey]"
  19. :key="item[labelKey]"
  20. >
  21. <template slot="title">
  22. <i :class="item[iconKey]"></i>
  23. <span
  24. slot="title"
  25. :class="{ 'el-menu--display': collapse && first }"
  26. >{{ generateTitleLab(item) }}</span
  27. >
  28. </template>
  29. <template v-for="(child, cindex) in item[childrenKey]">
  30. <el-menu-item
  31. :index="child[pathKey]"
  32. @click="open(child)"
  33. :class="{ 'is-active': vaildAvtive(child) }"
  34. v-if="validatenull(child[childrenKey])"
  35. :key="child[labelKey]"
  36. >
  37. <i :class="child[iconKey]"></i>
  38. <span slot="title">{{ generateTitleLab(child) }}</span>
  39. </el-menu-item>
  40. <sidebar-item
  41. v-else
  42. :menu="[child]"
  43. :key="cindex"
  44. :props="props"
  45. :screen="screen"
  46. :collapse="collapse"
  47. ></sidebar-item>
  48. </template>
  49. </el-submenu>
  50. </template>
  51. </div>
  52. </template>
  53. <script>
  54. import { mapGetters } from 'vuex'
  55. import { validatenull } from '@/utils/validate'
  56. import config from './config.js'
  57. import '../../../../public/static/sidebar/iconfont.css'
  58. import { getValue, generateTitle, getPath } from '@/utils/util'
  59. export default {
  60. name: 'sidebarItem',
  61. data() {
  62. return {
  63. config: config,
  64. }
  65. },
  66. props: {
  67. menu: {
  68. type: Array,
  69. },
  70. screen: {
  71. type: Number,
  72. },
  73. first: {
  74. type: Boolean,
  75. default: false,
  76. },
  77. props: {
  78. type: Object,
  79. default: () => {
  80. return {}
  81. },
  82. },
  83. collapse: {
  84. type: Boolean,
  85. },
  86. },
  87. created() {},
  88. mounted() {},
  89. computed: {
  90. ...mapGetters(['roles']),
  91. labelKey() {
  92. return this.props.label || this.config.propsDefault.label
  93. },
  94. pathKey() {
  95. return this.props.path || this.config.propsDefault.path
  96. },
  97. iconKey() {
  98. return this.props.icon || this.config.propsDefault.icon
  99. },
  100. childrenKey() {
  101. return this.props.children || this.config.propsDefault.children
  102. },
  103. nowTagValue() {
  104. return getValue(this.$route)
  105. },
  106. },
  107. methods: {
  108. generateTitleLab(item) {
  109. return generateTitle(item[this.labelKey], (item.meta || {}).title, this)
  110. },
  111. vaildAvtive(item) {
  112. const groupFlag = (item['group'] || []).some((ele) =>
  113. this.$route.path.includes(ele)
  114. )
  115. if (groupFlag)
  116. // console.info(groupFlag, 'groupFlag', item['group'], this.$route.path, this.nowTagValue, item[this.pathKey])
  117. return this.nowTagValue === item[this.pathKey] || groupFlag
  118. },
  119. vaildRoles(item) {
  120. item.meta = item.meta || {}
  121. return item.meta.roles ? item.meta.roles.includes(this.roles) : true
  122. },
  123. validatenull(val) {
  124. return validatenull(val)
  125. },
  126. open(item) {
  127. if (this.screen <= 1) this.$store.commit('SET_COLLAPSE')
  128. // this.$router.$winseaviewRouter.group = item.group;
  129. // this.$router.$winseaviewRouter.meta = item.meta;
  130. this.$router.push({
  131. path: getPath({
  132. name: item[this.labelKey],
  133. src: item[this.pathKey],
  134. i18n: (item.meta || {}).i18n,
  135. }),
  136. query: item.query,
  137. })
  138. },
  139. },
  140. }
  141. </script>