Skip to content

Commit 63ff44f

Browse files
authored
fix[Sidebar]: link bug (PanJiaChen#1134)
Fixed PanJiaChen#1125
1 parent dd5646f commit 63ff44f

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

src/views/layout/components/Sidebar/SidebarItem.vue

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<div v-if="!item.hidden&&item.children" class="menu-wrapper">
33

44
<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)">
66
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
77
<item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon||item.meta.icon" :title="generateTitle(onlyOneChild.meta.title)" />
88
</el-menu-item>
9-
</a>
9+
</app-link>
1010
</template>
1111

1212
<el-submenu v-else :index="item.name||item.path">
@@ -23,11 +23,11 @@
2323
:base-path="resolvePath(child.path)"
2424
class="nest-menu" />
2525

26-
<a v-else :href="child.path" :key="child.name" target="_blank" @click="clickLink(child.path,$event)">
26+
<app-link v-else :to="resolvePath(child.path)" :key="child.name">
2727
<el-menu-item :index="resolvePath(child.path)">
2828
<item v-if="child.meta" :icon="child.meta.icon" :title="generateTitle(child.meta.title)" />
2929
</el-menu-item>
30-
</a>
30+
</app-link>
3131
</template>
3232
</el-submenu>
3333

@@ -39,10 +39,11 @@ import path from 'path'
3939
import { generateTitle } from '@/utils/i18n'
4040
import { validateURL } from '@/utils/validate'
4141
import Item from './Item'
42+
import AppLink from './Link'
4243
4344
export default {
4445
name: 'SidebarItem',
45-
components: { Item },
46+
components: { Item, AppLink },
4647
props: {
4748
// route object
4849
item: {
@@ -89,18 +90,14 @@ export default {
8990
return false
9091
},
9192
resolvePath(routePath) {
93+
if (this.isExternalLink(routePath)) {
94+
return routePath
95+
}
9296
return path.resolve(this.basePath, routePath)
9397
},
9498
isExternalLink(routePath) {
9599
return validateURL(routePath)
96100
},
97-
clickLink(routePath, e) {
98-
if (!this.isExternalLink(routePath)) {
99-
e.preventDefault()
100-
const path = this.resolvePath(routePath)
101-
this.$router.push(path)
102-
}
103-
},
104101
generateTitle
105102
}
106103
}

0 commit comments

Comments
 (0)