Explorar o código

feature: [sidebar] add external-link

Pan %!s(int64=6) %!d(string=hai) anos
pai
achega
61394e573e

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 1 - 0
src/icons/svg/link.svg


+ 11 - 0
src/router/index.js

@@ -130,6 +130,17 @@ export const constantRouterMap = [
130 130
     ]
131 131
   },
132 132
 
133
+  {
134
+    path: 'external-link',
135
+    component: Layout,
136
+    children: [
137
+      {
138
+        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
139
+        meta: { title: 'externalLink', icon: 'link' }
140
+      }
141
+    ]
142
+  },
143
+
133 144
   { path: '*', redirect: '/404', hidden: true }
134 145
 ]
135 146
 

+ 29 - 0
src/views/layout/components/Sidebar/Item.vue

@@ -0,0 +1,29 @@
1
+<script>
2
+export default {
3
+  name: 'MenuItem',
4
+  functional: true,
5
+  props: {
6
+    icon: {
7
+      type: String,
8
+      default: ''
9
+    },
10
+    title: {
11
+      type: String,
12
+      default: ''
13
+    }
14
+  },
15
+  render(h, context) {
16
+    const { icon, title } = context.props
17
+    const vnodes = []
18
+
19
+    if (icon) {
20
+      vnodes.push(<svg-icon icon-class={icon}/>)
21
+    }
22
+
23
+    if (title) {
24
+      vnodes.push(<span slot='title'>{(title)}</span>)
25
+    }
26
+    return vnodes
27
+  }
28
+}
29
+</script>

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

@@ -1,17 +1,23 @@
1 1
 <template>
2 2
   <div v-if="!item.hidden&&item.children" class="menu-wrapper">
3 3
 
4
-    <router-link v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow" :to="resolvePath(onlyOneChild.path)">
5
-      <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
6
-        <svg-icon v-if="onlyOneChild.meta&&onlyOneChild.meta.icon" :icon-class="onlyOneChild.meta.icon"/>
7
-        <span v-if="onlyOneChild.meta&&onlyOneChild.meta.title" slot="title">{{ onlyOneChild.meta.title }}</span>
8
-      </el-menu-item>
9
-    </router-link>
4
+    <template v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow">
5
+      <a v-if="isExternalLink(onlyOneChild.path)" :href="onlyOneChild.path" target="blank">
6
+        apple
7
+        <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
8
+          <item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon" :title="onlyOneChild.meta.title" />
9
+        </el-menu-item>
10
+      </a>
11
+      <router-link v-else :to="resolvePath(onlyOneChild.path)">
12
+        <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
13
+          <item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon" :title="onlyOneChild.meta.title" />
14
+        </el-menu-item>
15
+      </router-link>
16
+    </template>
10 17
 
11 18
     <el-submenu v-else :index="item.name||item.path">
12 19
       <template slot="title">
13
-        <svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"/>
14
-        <span v-if="item.meta&&item.meta.title" slot="title">{{ item.meta.title }}</span>
20
+        <item v-if="item.meta" :icon="item.meta.icon" :title="item.meta.title" />
15 21
       </template>
16 22
 
17 23
       <template v-for="child in item.children" v-if="!child.hidden">
@@ -25,8 +31,7 @@
25 31
 
26 32
         <router-link v-else :to="resolvePath(child.path)" :key="child.name">
27 33
           <el-menu-item :index="resolvePath(child.path)">
28
-            <svg-icon v-if="child.meta&&child.meta.icon" :icon-class="child.meta.icon"/>
29
-            <span v-if="child.meta&&child.meta.title" slot="title">{{ child.meta.title }}</span>
34
+            <item v-if="child.meta" :icon="child.meta.icon" :title="child.meta.title" />
30 35
           </el-menu-item>
31 36
         </router-link>
32 37
       </template>
@@ -37,9 +42,12 @@
37 42
 
38 43
 <script>
39 44
 import path from 'path'
45
+import { validateURL } from '@/utils/validate'
46
+import Item from './Item'
40 47
 
41 48
 export default {
42 49
   name: 'SidebarItem',
50
+  components: { Item },
43 51
   props: {
44 52
     // route配置json
45 53
     item: {
@@ -76,8 +84,11 @@ export default {
76 84
       }
77 85
       return false
78 86
     },
79
-    resolvePath(...paths) {
80
-      return path.resolve(this.basePath, ...paths)
87
+    resolvePath(routePath) {
88
+      return path.resolve(this.basePath, routePath)
89
+    },
90
+    isExternalLink(routePath) {
91
+      return validateURL(routePath)
81 92
     }
82 93
   }
83 94
 }