Browse Source

fix: 修复直接通过浏览器地址栏打开演示环境标签详情页,tag未加载的情况

xiaoxian521 2 years ago
parent
commit
9ca764b2a6
3 changed files with 39 additions and 27 deletions
  1. 4 6
      src/views/tabs/detail.vue
  2. 32 0
      src/views/tabs/hooks.ts
  3. 3 21
      src/views/tabs/index.vue

+ 4 - 6
src/views/tabs/detail.vue

@@ -1,11 +1,9 @@
 <script setup lang="ts">
-import { useRoute } from "vue-router";
-const route = useRoute();
-const index = route.query?.id ?? -1;
+import { useDetail } from "./hooks";
+const { initToDetail, id } = useDetail();
+initToDetail();
 </script>
 
 <template>
-  <div>{{ index }} - 详情页内容在此</div>
+  <div>{{ id }} - 详情页内容在此</div>
 </template>
-
-<style lang="scss" scoped></style>

+ 32 - 0
src/views/tabs/hooks.ts

@@ -0,0 +1,32 @@
+import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
+import { useRouter, useRoute } from "vue-router";
+import { onBeforeMount } from "vue";
+
+export function useDetail() {
+  const route = useRoute();
+  const router = useRouter();
+  const id = route.query?.id ?? -1;
+
+  function toDetail(index: number | string | string[] | number[]) {
+    useMultiTagsStoreHook().handleTags("push", {
+      path: `/tabs/detail`,
+      parentPath: route.matched[0].path,
+      name: "tabDetail",
+      query: { id: String(index) },
+      meta: {
+        title: { zh: `No.${index} - 详情信息`, en: `No.${index} - DetailInfo` },
+        showLink: false,
+        dynamicLevel: 3
+      }
+    });
+    router.push({ name: "tabDetail", query: { id: String(index) } });
+  }
+
+  function initToDetail() {
+    onBeforeMount(() => {
+      if (id) toDetail(id);
+    });
+  }
+
+  return { toDetail, initToDetail, id };
+}

+ 3 - 21
src/views/tabs/index.vue

@@ -1,16 +1,16 @@
 <script setup lang="ts">
 import { ref, computed } from "vue";
 import { transformI18n } from "/@/plugins/i18n";
-import { useRouter, useRoute } from "vue-router";
 import { TreeSelect } from "@pureadmin/components";
 import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
-
+import { usePermissionStoreHook } from "/@/store/modules/permission";
 import {
   deleteChildren,
   appendFieldByUniqueId,
   getNodeByUniqueId
 } from "/@/utils/tree";
-import { usePermissionStoreHook } from "/@/store/modules/permission";
+import { useDetail } from "./hooks";
+const { toDetail } = useDetail();
 
 let treeData = computed(() => {
   return appendFieldByUniqueId(
@@ -20,26 +20,8 @@ let treeData = computed(() => {
   );
 });
 
-const route = useRoute();
-const router = useRouter();
-
 const value = ref<string[]>([]);
 
-function toDetail(index: number) {
-  useMultiTagsStoreHook().handleTags("push", {
-    path: `/tabs/detail`,
-    parentPath: route.matched[0].path,
-    name: "tabDetail",
-    query: { id: String(index) },
-    meta: {
-      title: { zh: `No.${index} - 详情信息`, en: `No.${index} - DetailInfo` },
-      showLink: false,
-      dynamicLevel: 3
-    }
-  });
-  router.push({ name: "tabDetail", query: { id: String(index) } });
-}
-
 function onCloseTags() {
   value.value.forEach(uniqueId => {
     let currentPath =