sidebarItem.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. console.log(this.menu)
  90. },
  91. computed: {
  92. ...mapGetters(['roles']),
  93. labelKey() {
  94. return this.props.label || this.config.propsDefault.label
  95. },
  96. pathKey() {
  97. return this.props.path || this.config.propsDefault.path
  98. },
  99. iconKey() {
  100. return this.props.icon || this.config.propsDefault.icon
  101. },
  102. childrenKey() {
  103. return this.props.children || this.config.propsDefault.children
  104. },
  105. nowTagValue() {
  106. return getValue(this.$route)
  107. },
  108. },
  109. methods: {
  110. generateTitleLab(item) {
  111. return generateTitle(item[this.labelKey], (item.meta || {}).title, this)
  112. },
  113. vaildAvtive(item) {
  114. // console.log(item)
  115. const groupFlag = (item['group'] || []).some((ele) =>
  116. this.$route.path.includes(ele)
  117. )
  118. console.log(groupFlag)
  119. if (groupFlag)
  120. // console.info(groupFlag, 'groupFlag', item['group'], this.$route.path, this.nowTagValue, item[this.pathKey])
  121. return this.nowTagValue === item[this.pathKey] || groupFlag
  122. },
  123. vaildRoles(item) {
  124. item.meta = item.meta || {}
  125. return item.meta.roles ? item.meta.roles.includes(this.roles) : true
  126. },
  127. validatenull(val) {
  128. return validatenull(val)
  129. },
  130. open(item) {
  131. if (this.screen <= 1) this.$store.commit('SET_COLLAPSE')
  132. // this.$router.$winseaviewRouter.group = item.group;
  133. // this.$router.$winseaviewRouter.meta = item.meta;
  134. this.$router.push({
  135. path: getPath({
  136. name: item[this.labelKey],
  137. src: item[this.pathKey],
  138. i18n: (item.meta || {}).i18n,
  139. }),
  140. query: item.query,
  141. })
  142. },
  143. },
  144. }
  145. </script>