tab.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <div class="tab" ref="tabChange">
  3. <div class="dflex h100">
  4. <div class="tab_content dflex_c h100 margin_r_10 crpr" v-for="(item, i) in tagsList"
  5. :class="{ active: isActive(item.path) }" :key="i">
  6. <router-link :to="item.path" class="tab_name h100 dflex_c">{{ item.title }}</router-link>
  7. <span class="el-icon-close" v-if="item.close" @click="closeTags(i)"></span>
  8. </div>
  9. </div>
  10. <el-dropdown class="tab_btn" trigger="click" @command="handleTags">
  11. <el-button size="mini" class="h100">
  12. 标签选项
  13. <i class="el-icon-arrow-down margin_l_5"></i>
  14. </el-button>
  15. <el-dropdown-menu slot="dropdown">
  16. <el-dropdown-item command="other">关闭其他</el-dropdown-item>
  17. <el-dropdown-item command="all">关闭所有</el-dropdown-item>
  18. </el-dropdown-menu>
  19. </el-dropdown>
  20. </div>
  21. </template>
  22. <script>
  23. import bus from '@/common/bus';
  24. import config from '@/admin.config.js';
  25. export default {
  26. data() {
  27. return {
  28. tagsList: [],
  29. home: {},
  30. tagTotal: 12
  31. };
  32. },
  33. watch: {
  34. $route(newValue) {
  35. if (newValue.path == '/preview-image') return;
  36. this.setTags(newValue);
  37. }
  38. },
  39. methods: {
  40. isActive(path) {
  41. return path === this.$route.fullPath;
  42. },
  43. closeTags(index) {
  44. const delItem = this.tagsList.splice(index, 1)[0];
  45. const item = this.tagsList[index] ? this.tagsList[index] : this.tagsList[index - 1];
  46. if (item) {
  47. delItem.path === this.$route.fullPath && this.$router.push(item.path);
  48. } else {
  49. this.$router.push(config.index.url);
  50. }
  51. localStorage.setItem('sys-tags', JSON.stringify(this.tagsList));
  52. },
  53. setTags(route) {
  54. const curPath = route.fullPath;
  55. if (curPath == '/') {
  56. this.$router.push(config.index.url);
  57. return;
  58. }
  59. if (['/', '/pages/login', '/pages/error/404'].includes(curPath)) {
  60. return;
  61. }
  62. const isExist = this.tagsList.some(item => {
  63. return item.path === curPath || item.title == route.query.tab;
  64. });
  65. var menus = uni.getStorageSync('__menu');
  66. if (!isExist) {
  67. if (this.tagsList.length >= this.tagTotal) {
  68. this.tagsList.splice(1, 1);
  69. }
  70. if (menus && menus.length > 0) {
  71. const menu = menus.find(x => x.module_url == route.path) || {
  72. name: route.query.tab
  73. };
  74. if (menu) {
  75. this.tagsList.push({
  76. title: menu.module_name || menu.name,
  77. path: curPath,
  78. close: !0
  79. });
  80. }
  81. }
  82. } else {
  83. this.tagsList[this.tagsList.findIndex(x => x.path === curPath || x.title == route.query.tab)].path =
  84. curPath;
  85. }
  86. localStorage.setItem('sys-tags', JSON.stringify(this.tagsList));
  87. },
  88. handleTags(command) {
  89. command === 'other' ? this.closeOther() : this.closeAll();
  90. },
  91. closeAll() {
  92. this.tagsList = [];
  93. this.tagsList.push(this.home);
  94. this.$router.push(config.index.url);
  95. localStorage.setItem('sys-tags', JSON.stringify(this.tagsList));
  96. },
  97. closeOther() {
  98. const curItem = this.tagsList.find(item => {
  99. return item.path === this.$route.fullPath;
  100. });
  101. this.tagsList = [];
  102. this.tagsList.push(this.home);
  103. if (curItem.path !== this.home.path) this.tagsList.push(curItem);
  104. localStorage.setItem('sys-tags', JSON.stringify(this.tagsList));
  105. }
  106. },
  107. created() {
  108. this.home = {
  109. title: '系统首页',
  110. path: config.index.url,
  111. close: 0
  112. };
  113. bus.$on('logOut', () => {
  114. this.tagsList = [];
  115. this.tagsList.push(this.home);
  116. });
  117. bus.$on('fold', res => {
  118. if (res) {
  119. this.$refs.tabChange.style.left = '80px';
  120. } else {
  121. this.$refs.tabChange.style.left = 'calc(16px + var(--window-left))';
  122. }
  123. });
  124. bus.$on('tab-close', res => {
  125. res = res || this.$route.fullPath;
  126. if (res) {
  127. this.tagsList.forEach((tag, index) => {
  128. if (tag.path == res) {
  129. this.tagsList.splice(index, 1);
  130. return false;
  131. }
  132. });
  133. }
  134. });
  135. let tags = localStorage.getItem('sys-tags');
  136. if (tags) {
  137. this.tagsList = JSON.parse(tags);
  138. } else {
  139. this.tagsList.push(this.home);
  140. }
  141. this.setTags(this.$route);
  142. }
  143. };
  144. </script>
  145. <style>
  146. .tab {
  147. position: fixed;
  148. top: 76px;
  149. left: 196px;
  150. right: 16px;
  151. overflow: hidden;
  152. transition: all 0.3s ease-in-out;
  153. z-index: 10;
  154. height: 36px;
  155. }
  156. .tab_content {
  157. border-radius: 4px;
  158. font-size: 12px;
  159. background: #fff;
  160. }
  161. .tab_content:not(.active):hover {
  162. background: #e6e6e6;
  163. }
  164. .tab_content.active {
  165. color: #fff;
  166. background: #8a8a8a;
  167. }
  168. .tab_content.active a {
  169. color: #fff;
  170. }
  171. .tab_content .el-icon-close {
  172. position: relative;
  173. left: -5px;
  174. padding: 1px;
  175. font-size: 15px;
  176. transform: scale(0.8);
  177. }
  178. .tab_content .el-icon-close:hover {
  179. background-color: #fff;
  180. color: #8a8a8a;
  181. border-radius: 50%;
  182. }
  183. .tab_name {
  184. padding: 0 10px;
  185. overflow: hidden;
  186. white-space: nowrap;
  187. text-overflow: ellipsis;
  188. color: #666;
  189. }
  190. .tab .el-button {
  191. border: none;
  192. }
  193. .tab_btn {
  194. position: absolute;
  195. right: 0;
  196. top: 0;
  197. box-sizing: border-box;
  198. text-align: center;
  199. z-index: 10;
  200. height: 100%;
  201. }
  202. </style>