Kaynağa Gözat

fix[Sidebar]: link bug

Pan 6 yıl önce
ebeveyn
işleme
1ae92fe958

+ 39 - 0
src/views/layout/components/Sidebar/Link.vue

@@ -0,0 +1,39 @@
1
+
2
+<template>
3
+  <!-- eslint-disable vue/require-component-is-->
4
+  <component v-bind="linkProps(to)">
5
+    <slot/>
6
+  </component>
7
+</template>
8
+
9
+<script>
10
+import { validateURL } from '@/utils/validate'
11
+
12
+export default {
13
+  props: {
14
+    to: {
15
+      type: String,
16
+      required: true
17
+    }
18
+  },
19
+  methods: {
20
+    isExternalLink(routePath) {
21
+      return validateURL(routePath)
22
+    },
23
+    linkProps(url) {
24
+      if (this.isExternalLink(url)) {
25
+        return {
26
+          is: 'a',
27
+          href: url,
28
+          target: '_blank',
29
+          rel: 'noopener'
30
+        }
31
+      }
32
+      return {
33
+        is: 'router-link',
34
+        to: url
35
+      }
36
+    }
37
+  }
38
+}
39
+</script>

+ 9 - 12
src/views/layout/components/Sidebar/SidebarItem.vue

@@ -2,11 +2,11 @@
2 2
   <div v-if="!item.hidden&&item.children" class="menu-wrapper">
3 3
 
4 4
     <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
5
-      <a :href="onlyOneChild.path" target="_blank" @click="clickLink(onlyOneChild.path,$event)">
5
+      <app-link :to="resolvePath(onlyOneChild.path)">
6 6
         <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
7 7
           <item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon||item.meta.icon" :title="onlyOneChild.meta.title" />
8 8
         </el-menu-item>
9
-      </a>
9
+      </app-link>
10 10
     </template>
11 11
 
12 12
     <el-submenu v-else :index="item.name||item.path">
@@ -22,11 +22,11 @@
22 22
           :key="child.path"
23 23
           :base-path="resolvePath(child.path)"
24 24
           class="nest-menu" />
25
-        <a v-else :href="child.path" :key="child.name" target="_blank" @click="clickLink(child.path,$event)">
25
+        <app-link v-else :to="resolvePath(child.path)" :key="child.name">
26 26
           <el-menu-item :index="resolvePath(child.path)">
27 27
             <item v-if="child.meta" :icon="child.meta.icon" :title="child.meta.title" />
28 28
           </el-menu-item>
29
-        </a>
29
+        </app-link>
30 30
       </template>
31 31
     </el-submenu>
32 32
 
@@ -37,10 +37,11 @@
37 37
 import path from 'path'
38 38
 import { validateURL } from '@/utils/validate'
39 39
 import Item from './Item'
40
+import AppLink from './Link'
40 41
 
41 42
 export default {
42 43
   name: 'SidebarItem',
43
-  components: { Item },
44
+  components: { Item, AppLink },
44 45
   props: {
45 46
     // route object
46 47
     item: {
@@ -87,17 +88,13 @@ export default {
87 88
       return false
88 89
     },
89 90
     resolvePath(routePath) {
91
+      if (this.isExternalLink(routePath)) {
92
+        return routePath
93
+      }
90 94
       return path.resolve(this.basePath, routePath)
91 95
     },
92 96
     isExternalLink(routePath) {
93 97
       return validateURL(routePath)
94
-    },
95
-    clickLink(routePath, e) {
96
-      if (!this.isExternalLink(routePath)) {
97
-        e.preventDefault()
98
-        const path = this.resolvePath(routePath)
99
-        this.$router.push(path)
100
-      }
101 98
     }
102 99
   }
103 100
 }