Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: [sidebar] add external-link #991

Merged
merged 2 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/icons/svg/link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export default {
exportZip: 'Export Zip',
theme: 'Theme',
clipboardDemo: 'Clipboard',
i18n: 'I18n'
i18n: 'I18n',
externalLink: 'External Link'
},
navbar: {
logOut: 'Log Out',
Expand Down
3 changes: 2 additions & 1 deletion src/lang/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export default {
exportZip: 'Export Zip',
theme: '换肤',
clipboardDemo: 'Clipboard',
i18n: '国际化'
i18n: '国际化',
externalLink: '外链'
},
navbar: {
logOut: '退出登录',
Expand Down
11 changes: 11 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,16 @@ export const asyncRouterMap = [
]
},

{
path: 'external-link',
component: Layout,
children: [
{
path: 'https://github.com/PanJiaChen/vue-element-admin',
meta: { title: 'externalLink', icon: 'link' }
}
]
},

{ path: '*', redirect: '/404', hidden: true }
]
29 changes: 29 additions & 0 deletions src/views/layout/components/Sidebar/Item.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
export default {
name: 'MenuItem',
functional: true,
props: {
icon: {
type: String,
default: ''
},
title: {
type: String,
default: ''
}
},
render(h, context) {
const { icon, title } = context.props
const vnodes = []

if (icon) {
vnodes.push(<svg-icon icon-class={icon}/>)
}

if (title) {
vnodes.push(<span slot='title'>{(title)}</span>)
}
return vnodes
}
}
</script>
36 changes: 23 additions & 13 deletions src/views/layout/components/Sidebar/SidebarItem.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
<template>
<div v-if="!item.hidden&&item.children" class="menu-wrapper">

<router-link v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow" :to="resolvePath(onlyOneChild.path)">
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
<svg-icon v-if="onlyOneChild.meta&&onlyOneChild.meta.icon" :icon-class="onlyOneChild.meta.icon"/>
<span v-if="onlyOneChild.meta&&onlyOneChild.meta.title" slot="title">{{ generateTitle(onlyOneChild.meta.title) }}</span>
</el-menu-item>
</router-link>
<template v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow">
<a v-if="isExternalLink(onlyOneChild.path)" :href="onlyOneChild.path" target="blank">
apple
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
<item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon" :title="generateTitle(onlyOneChild.meta.title)" />
</el-menu-item>
</a>
<router-link v-else :to="resolvePath(onlyOneChild.path)">
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
<item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon" :title="generateTitle(onlyOneChild.meta.title)" />
</el-menu-item>
</router-link>
</template>

<el-submenu v-else :index="item.name||item.path">
<template slot="title">
<svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"/>
<span v-if="item.meta&&item.meta.title" slot="title">{{ generateTitle(item.meta.title) }}</span>
<item v-if="item.meta" :icon="item.meta.icon" :title="generateTitle(item.meta.title)" />
</template>

<template v-for="child in item.children" v-if="!child.hidden">
<sidebar-item v-if="child.children&&child.children.length>0" :is-nest="true" :item="child" :key="child.path" :base-path="resolvePath(child.path)" class="nest-menu"/>

<router-link v-else :to="resolvePath(child.path)" :key="child.name">
<el-menu-item :index="resolvePath(child.path)">
<svg-icon v-if="child.meta&&child.meta.icon" :icon-class="child.meta.icon"/>
<span v-if="child.meta&&child.meta.title" slot="title">{{ generateTitle(child.meta.title) }}</span>
<item v-if="child.meta" :icon="child.meta.icon" :title="generateTitle(child.meta.title)" />
</el-menu-item>
</router-link>
</template>
Expand All @@ -32,9 +37,12 @@
<script>
import path from 'path'
import { generateTitle } from '@/utils/i18n'
import { validateURL } from '@/utils/validate'
import Item from './Item'

export default {
name: 'SidebarItem',
components: { Item },
props: {
// route object
item: {
Expand Down Expand Up @@ -71,11 +79,13 @@ export default {
}
return false
},
resolvePath(...paths) {
return path.resolve(this.basePath, ...paths)
resolvePath(routePath) {
return path.resolve(this.basePath, routePath)
},
isExternalLink(routePath) {
return validateURL(routePath)
},
generateTitle
}
}
</script>