sidebarItem.vue 3.4 KB

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