tab.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import router from '@/router';
  2. import { RouteLocationMatched, RouteLocationNormalized, RouteLocationRaw } from 'vue-router';
  3. import { useTagsViewStore } from '@/store/modules/tagsView';
  4. export default {
  5. /**
  6. * 刷新当前tab页签
  7. * @param obj 标签对象
  8. */
  9. async refreshPage(obj?: RouteLocationNormalized): Promise<void> {
  10. const { path, query, matched } = router.currentRoute.value;
  11. if (obj === undefined) {
  12. matched.forEach((m: RouteLocationMatched) => {
  13. if (m.components && m.components.default && m.components.default.name) {
  14. if (!['Layout', 'ParentView'].includes(m.components.default.name)) {
  15. obj = {
  16. name: m.components.default.name,
  17. path: path,
  18. query: query,
  19. matched: undefined,
  20. fullPath: undefined,
  21. hash: undefined,
  22. params: undefined,
  23. redirectedFrom: undefined,
  24. meta: undefined
  25. };
  26. }
  27. }
  28. });
  29. }
  30. let query1: undefined | {} = {};
  31. let path1: undefined | string = '';
  32. if (obj) {
  33. query1 = obj.query;
  34. path1 = obj.path;
  35. }
  36. await useTagsViewStore().delCachedView(obj);
  37. await router.replace({
  38. path: '/redirect' + path1,
  39. query: query1
  40. });
  41. },
  42. // 关闭当前tab页签,打开新页签
  43. closeOpenPage(obj: RouteLocationRaw): void {
  44. useTagsViewStore().delView(router.currentRoute.value);
  45. if (obj !== undefined) {
  46. router.push(obj);
  47. }
  48. },
  49. // 关闭指定tab页签
  50. async closePage(obj?: RouteLocationNormalized): Promise<{ visitedViews: RouteLocationNormalized[]; cachedViews: string[] } | any> {
  51. if (obj === undefined) {
  52. // prettier-ignore
  53. const { visitedViews } = await useTagsViewStore().delView(router.currentRoute.value)
  54. const latestView = visitedViews.slice(-1)[0];
  55. if (latestView) {
  56. return router.push(latestView.fullPath);
  57. }
  58. return router.push('/');
  59. }
  60. return useTagsViewStore().delView(obj);
  61. },
  62. // 关闭所有tab页签
  63. closeAllPage() {
  64. return useTagsViewStore().delAllViews();
  65. },
  66. // 关闭左侧tab页签
  67. closeLeftPage(obj?: RouteLocationNormalized) {
  68. return useTagsViewStore().delLeftTags(obj || router.currentRoute.value);
  69. },
  70. // 关闭右侧tab页签
  71. closeRightPage(obj?: RouteLocationNormalized) {
  72. return useTagsViewStore().delRightTags(obj || router.currentRoute.value);
  73. },
  74. // 关闭其他tab页签
  75. closeOtherPage(obj?: RouteLocationNormalized) {
  76. return useTagsViewStore().delOthersViews(obj || router.currentRoute.value);
  77. },
  78. /**
  79. * 打开tab页签
  80. * @param url 路由地址
  81. * @param title 标题
  82. * @param query 参数
  83. */
  84. openPage(url: string, title?: string, query?: any) {
  85. const obj = { path: url, query: { ...query, title } };
  86. return router.push(obj);
  87. },
  88. /**
  89. * 修改tab页签
  90. * @param obj 标签对象
  91. */
  92. updatePage(obj: RouteLocationNormalized) {
  93. return useTagsViewStore().updateVisitedView(obj);
  94. }
  95. };