breadcrumb.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div class="layout-navbars-breadcrumb">
  3. <!-- {{[...breadCrumbList,...crumbPast]}} -->
  4. <i v-if="collapseShow" class="layout-navbars-breadcrumb-icon"
  5. :class="getThemeConfig.isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" @click="onThemeConfigChange"></i>
  6. <el-breadcrumb class="layout-navbars-breadcrumb-hide" v-if="isShowcrumb" :style="{ display: isShowBreadcrumb }">
  7. <transition-group name="breadcrumb" mode="out-in">
  8. <el-breadcrumb-item v-for="(v, k) in [...breadCrumbList, ...crumbPast]" :key="v.path">
  9. <span v-if="k == 1" class="layout-navbars-breadcrumb-span">
  10. <Icon :type="v.icon" class="ivu-icon layout-navbars-breadcrumb-iconfont"
  11. v-if="getThemeConfig.isBreadcrumbIcon" />{{ $t(v.title) }}
  12. </span>
  13. <a v-else @click.prevent="onBreadcrumbClick(v)">
  14. <Icon :type="v.icon" class="ivu-icon layout-navbars-breadcrumb-iconfont"
  15. v-if="getThemeConfig.isBreadcrumbIcon" />{{ $t(v.title) }}
  16. </a>
  17. </el-breadcrumb-item>
  18. </transition-group>
  19. </el-breadcrumb>
  20. </div>
  21. </template>
  22. <script>
  23. import {
  24. Local
  25. } from '@/utils/storage.js';
  26. import {
  27. R
  28. } from '@/libs/util';
  29. import {
  30. getMenuopen
  31. } from '@/libs/util';
  32. export default {
  33. name: 'layoutBreadcrumb',
  34. data() {
  35. return {
  36. breadcrumbList: [],
  37. routeSplit: [],
  38. routeSplitFirst: '',
  39. routeSplitIndex: 1,
  40. };
  41. },
  42. computed: {
  43. breadCrumbList() {
  44. let menuList = this.$store.state.menus.menusName;
  45. let openMenus = getMenuopen(this.$route, menuList);
  46. let allMenuList = R(menuList, []);
  47. let selectMenu = [];
  48. if (allMenuList.length > 0) {
  49. openMenus.forEach((i) => {
  50. allMenuList.forEach((a) => {
  51. if (i === a.path) {
  52. selectMenu.push(a);
  53. }
  54. });
  55. });
  56. }
  57. return selectMenu;
  58. },
  59. crumbPast() {
  60. let that = this;
  61. let menuList = that.$store.state.menus.menusName;
  62. let allMenuList = R(menuList, []);
  63. let selectMenu = [];
  64. if (allMenuList.length > 0) {
  65. allMenuList.forEach((a) => {
  66. if (that.$route.path === a.path) {
  67. selectMenu.push(a);
  68. }
  69. });
  70. }
  71. return selectMenu;
  72. },
  73. // 获取布局配置信息
  74. getThemeConfig() {
  75. return this.$store.state.themeConfig.themeConfig;
  76. },
  77. // 动态设置经典、横向布局不显示
  78. isShowBreadcrumb() {
  79. const {
  80. layout,
  81. isBreadcrumb
  82. } = this.$store.state.themeConfig.themeConfig;
  83. if (layout === 'transverse' || layout === 'classic') {
  84. return 'none';
  85. } else {
  86. return isBreadcrumb ? '' : 'none';
  87. }
  88. },
  89. isShowcrumb() {
  90. const {
  91. layout
  92. } = this.$store.state.themeConfig.themeConfig;
  93. if (layout === 'transverse' || layout === 'classic') {
  94. return false;
  95. } else {
  96. return true;
  97. }
  98. },
  99. collapseShow() {
  100. return ['defaults', 'columns'].includes(this.$store.state.themeConfig.themeConfig.layout);
  101. },
  102. },
  103. mounted() {
  104. this.initRouteSplit(this.$route.path);
  105. },
  106. methods: {
  107. // breadcrumb 当前项点击时
  108. onBreadcrumbClick(v) {
  109. const {
  110. redirect,
  111. path
  112. } = v;
  113. if (redirect) this.$router.push(redirect);
  114. else this.$router.push(path);
  115. },
  116. // breadcrumb icon 点击菜单展开与收起
  117. onThemeConfigChange() {
  118. if (
  119. this.$store.state.themeConfig.themeConfig.layout == 'columns' &&
  120. !this.$store.state.menus.childMenuList.length &&
  121. this.$store.state.themeConfig.themeConfig.isCollapse
  122. ) {
  123. return;
  124. }
  125. this.$store.state.themeConfig.themeConfig.isCollapse = !this.$store.state.themeConfig.themeConfig
  126. .isCollapse;
  127. this.setLocalThemeConfig();
  128. },
  129. // 存储布局配置
  130. setLocalThemeConfig() {
  131. Local.remove('themeConfigPrev');
  132. Local.set('themeConfigPrev', this.$store.state.themeConfig.themeConfig);
  133. },
  134. // 递归设置 breadcrumb
  135. getBreadcrumbList(arr) {
  136. arr.map((item) => {
  137. this.routeSplit.map((v, k, arrs) => {
  138. if (this.routeSplitFirst === item.path) {
  139. this.routeSplitFirst += `/${arrs[this.routeSplitIndex]}`;
  140. this.breadcrumbList.push(item);
  141. this.routeSplitIndex++;
  142. if (item.children) this.getBreadcrumbList(item.children);
  143. }
  144. });
  145. });
  146. },
  147. // 当前路由分割处理
  148. initRouteSplit(path) {
  149. this.breadcrumbList = [{
  150. path: '/',
  151. meta: {
  152. title: this.$store.state.routesList.routesList[0].title,
  153. icon: this.$store.state.routesList.routesList[0].icon,
  154. },
  155. }, ];
  156. // this.routeSplit = path.split('/');
  157. // this.routeSplit.shift();
  158. this.routeSplitFirst = path;
  159. this.routeSplitIndex = 1;
  160. this.getBreadcrumbList(this.$store.state.routesList.routesList);
  161. },
  162. },
  163. // 监听路由的变化
  164. watch: {
  165. $route: {
  166. handler(newVal) {
  167. // this.initRouteSplit(newVal.path);
  168. let menuList = this.$store.state.menus.menusName;
  169. let openMenus = getMenuopen(newVal, menuList);
  170. let allMenuList = R(menuList, []);
  171. let selectMenu = [];
  172. if (allMenuList.length > 0) {
  173. openMenus.forEach((i) => {
  174. allMenuList.forEach((a) => {
  175. if (i === a.path) {
  176. selectMenu.push(a);
  177. }
  178. });
  179. });
  180. }
  181. },
  182. deep: true,
  183. },
  184. },
  185. };
  186. </script>
  187. <style scoped lang="scss">
  188. .layout-navbars-breadcrumb {
  189. // flex: 1;
  190. height: inherit;
  191. display: flex;
  192. align-items: center;
  193. padding-left: 15px;
  194. .layout-navbars-breadcrumb-icon {
  195. cursor: pointer;
  196. font-size: 18px;
  197. margin-right: 15px;
  198. color: var(--prev-bg-topBarColor);
  199. opacity: 0.8;
  200. &:hover {
  201. opacity: 1;
  202. }
  203. }
  204. .layout-navbars-breadcrumb-span {
  205. opacity: 0.7;
  206. color: var(--prev-bg-topBarColor);
  207. }
  208. .layout-navbars-breadcrumb-iconfont {
  209. font-size: 14px;
  210. margin-right: 5px;
  211. }
  212. }
  213. </style>