feat:动态路由样式调整

This commit is contained in:
有来技术
2021-11-28 22:49:07 +08:00
parent b048d19d22
commit 1d21c6b098
15 changed files with 146 additions and 150 deletions

View File

@@ -20,6 +20,7 @@
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.7", "@types/node": "^16.11.7",
"@types/nprogress": "^0.2.0", "@types/nprogress": "^0.2.0",
"@types/path-browserify": "^1.0.0",
"@vitejs/plugin-vue": "^1.9.3", "@vitejs/plugin-vue": "^1.9.3",
"sass": "^1.43.4", "sass": "^1.43.4",
"typescript": "^4.4.3", "typescript": "^4.4.3",

View File

@@ -2,7 +2,7 @@ import request from '@/utils/request'
export function getRouteList() { export function getRouteList() {
return request({ return request({
url: '/youlai-admin/api/v1/menus/route', url: '/youlai-admin/api/v2/menus/route',
method: 'get' method: 'get'
}) })
} }

View File

@@ -64,7 +64,7 @@ export default {
return store.state.app.sidebar return store.state.app.sidebar
}) })
const device = computed(() => { const device = computed(() => {
return store.state.app.device.toString() return store.state.app.device
}) })
const avatar = computed(() => { const avatar = computed(() => {
return store.state.user.avatar return store.state.user.avatar

View File

@@ -52,7 +52,7 @@ export default defineComponent({
}) })
watch(() => state.sidebarLogo, (value) => { watch(() => state.sidebarLogo, (value) => {
store.dispatch('settings/changeSetting', { key: 'showSidebarLogo', value }) store.dispatch('settings/changeSetting', { key: 'sidebarLogo', value })
}) })
return { return {

View File

@@ -1,43 +1,40 @@
<template> <template>
<component :is="type" v-bind="linkProps(to)"> <a
v-if="isExternal(to)"
:href="to"
target="_blank"
rel="noopener"
>
<slot /> <slot />
</component> </a>
<div
v-else
@click="push"
>
<slot />
</div>
</template> </template>
<script> <script lang="ts">
import { defineComponent } from 'vue'
import { isExternal } from '@/utils/validate' import { isExternal } from '@/utils/validate'
import { useRouter } from 'vue-router'
export default { export default defineComponent({
props: { props: {
to: { to: {
type: String, type: String,
required: true required: true
} }
}, },
computed: { setup(props) {
isExternal() { const router = useRouter()
return isExternal(this.to) const push = () => {
}, router.push(props.to)
type() {
if (this.isExternal) {
return 'a'
}
return 'router-link'
} }
}, return {
methods: { push,
linkProps(to) { isExternal
if (this.isExternal) {
return {
href: to,
target: '_blank',
rel: 'noopener'
}
}
return {
to: to
}
} }
} }
} })
</script> </script>

View File

@@ -1,47 +1,53 @@
<template> <template>
<div v-if="!item.hidden"> <div>
<template <div v-if="!item.meta ||!item.meta.hidden">
v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow"> <!-- 非嵌套路由 -->
<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)"> <template
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}"> v-if="!item.children||item.children.length==0">
<svg-icon v-if="onlyOneChild.meta && onlyOneChild.meta.icon" :icon-class="onlyOneChild.meta.icon"></svg-icon> <app-link v-if="item.meta" :to="resolvePath(item.path)">
<span v-if="onlyOneChild.meta && onlyOneChild.meta.title">{{ onlyOneChild.meta.title }}</span> <el-menu-item :index="resolvePath(item.path)" :class="{'submenu-title-noDropdown':!isNest}">
</el-menu-item> <svg-icon v-if="item.meta && item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
</app-link> <span v-if="item.meta && item.meta.title">{{ item.meta.title }}</span>
</template> </el-menu-item>
</app-link>
<el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
<template slot="title">
<svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
<span v-if="item.meta && item.meta.title">{{ item.meta.title }}</span>
</template> </template>
<sidebar-item <!-- 嵌套路由 -->
v-for="child in item.children" <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
:key="child.path" <template #title>
:is-nest="true" <svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
:item="child" <span v-if="item.meta && item.meta.title">{{ item.meta.title }}</span>
:base-path="resolvePath(child.path)" </template>
class="nest-menu"
/> <sidebar-item
</el-sub-menu> v-for="child in item.children"
:key="child.path"
:is-nest="true"
:item="child"
:base-path="resolvePath(child.path)"
class="nest-menu"
/>
</el-sub-menu>
</div>
</div> </div>
</template> </template>
<script> <script lang="ts">
import path from 'path-browserify' import path from 'path-browserify'
import {defineComponent, PropType} from "vue";
import {RouteRecordRaw} from 'vue-router'
import {isExternal} from '@utils/validate' import {isExternal} from '@utils/validate'
import AppLink from './Link.vue' import AppLink from './Link.vue'
import SvgIcon from '@/components/SvgIcon/index.vue'; import SvgIcon from '@/components/SvgIcon/index.vue';
export default {
export default defineComponent({
name: 'SidebarItem', name: 'SidebarItem',
components: {SvgIcon, AppLink}, components: {SvgIcon, AppLink},
props: { props: {
// route object
item: { item: {
type: Object, type: Object as PropType<RouteRecordRaw>,
required: true required: true
}, },
isNest: { isNest: {
@@ -53,46 +59,22 @@ export default {
default: '' default: ''
} }
}, },
data() { setup(props) {
// To fix https://github.com/PanJiaChen/vue-admin-template/issues/237 const resolvePath = (routePath: string) => {
// TODO: refactor with render function
this.onlyOneChild = null
return {}
},
methods: {
hasOneShowingChild(children = [], parent) {
const showingChildren = children.filter(item => {
if (item.hidden) {
return false
} else {
// Temp set(will be used if only has one showing child)
this.onlyOneChild = item
return true
}
})
// When there is only one child router, the child router is displayed by default
if (showingChildren.length === 1) {
return true
}
// Show parent if there are no child router to display
if (showingChildren.length === 0) {
this.onlyOneChild = {...parent, path: '', noShowingChildren: true}
return true
}
return false
},
resolvePath(routePath) {
if (isExternal(routePath)) { if (isExternal(routePath)) {
return routePath return routePath
} }
if (isExternal(this.basePath)) { if (isExternal(props.basePath)) {
return this.basePath return props.basePath
} }
return path.resolve(this.basePath, routePath) console.log(props.basePath,routePath)
return path.resolve(props.basePath, routePath)
}
return {
resolvePath
} }
} }
} })
</script>
</script>

View File

@@ -1,29 +1,38 @@
<template> <template>
<div :class="{'has-logo':showLogo}"> <div :class="{'has-logo':showLogo}">
<logo v-if="showLogo" :collapse="isCollapse"/> <logo v-if="showLogo" :collapse="isCollapse"/>
<!-- :background-color="variables.menuBg"
:text-color="variables.menuText"
:active-text-color="variables.menuActiveText"-->
<el-scrollbar wrap-class="scrollbar-wrapper"> <el-scrollbar wrap-class="scrollbar-wrapper">
<el-menu <el-menu
:default-active="activeMenu" :default-active="activeMenu"
:collapse="isCollapse" :collapse="isCollapse"
:background-color="variables.menuBg" background-color="#304156"
:text-color="variables.menuText" text-color="#bfcbd9"
active-text-color="#409EFF"
:unique-opened="false" :unique-opened="false"
:active-text-color="variables.menuActiveText"
:collapse-transition="false" :collapse-transition="false"
mode="vertical" mode="vertical">
> <sidebar-item
<sidebar-item v-for="route in routes" :key="route.path" :item="route" v-for="route in routes"
:base-path="route.path"/> :item="route"
:key="route.path"
:base-path="route.path"
/>
</el-menu> </el-menu>
</el-scrollbar> </el-scrollbar>
</div> </div>
</template> </template>
<script> <script lang="ts">
import {computed, defineComponent} from "vue"; import {computed, defineComponent} from "vue";
import SidebarItem from './SidebarItem.vue' import SidebarItem from './SidebarItem.vue'
import Logo from './Logo.vue' import Logo from './Logo.vue'
import variables from '@styles/variables.scss' import variables from '@/styles/variables.scss'
import {useStore} from '@/store' import {useStore} from '@/store'
import {useRoute} from 'vue-router' import {useRoute} from 'vue-router'
@@ -33,17 +42,11 @@ export default defineComponent({
Logo Logo
}, },
setup() { setup() {
const store = useStore() const store = useStore();
const route = useRoute() const route = useRoute()
const sidebar = computed(() => { const sidebar = computed(() => store.state.app.sidebar)
return store.state.app.sidebar const routes = computed(() => store.state.permission.routes)
}) const showLogo = computed(() => store.state.settings.sidebarLogo)
const routes = computed(() => {
return store.state.permission.routes
})
const showLogo = computed(() => {
return store.state.settings.sidebarLogo
})
const activeMenu = computed(() => { const activeMenu = computed(() => {
const {meta, path} = route const {meta, path} = route
// if set path, the sidebar will highlight the path you set // if set path, the sidebar will highlight the path you set
@@ -52,9 +55,7 @@ export default defineComponent({
} }
return path return path
}) })
const isCollapse = computed(() => { const isCollapse = computed(() => !store.state.app.sidebar.opened)
return !sidebar.value.opened
})
return { return {
sidebar, sidebar,
@@ -62,7 +63,7 @@ export default defineComponent({
showLogo, showLogo,
variables, variables,
activeMenu, activeMenu,
isCollapse isCollapse,
} }
} }
}) })

View File

@@ -9,10 +9,10 @@
</div> </div>
<app-main/> <app-main/>
<!-- <!--
<right-panel v-if="showSettings"> <right-panel v-if="showSettings">
<settings/> <settings/>
</right-panel> </right-panel>
--> -->
</div> </div>
</div> </div>
</template> </template>
@@ -23,6 +23,7 @@ import {computed, defineComponent, onBeforeMount, onBeforeUnmount, onMounted, re
import {AppMain, Navbar, Settings, Sidebar, TagsView} from './components/index.ts' import {AppMain, Navbar, Settings, Sidebar, TagsView} from './components/index.ts'
import {useStore} from "@store"; import {useStore} from "@store";
import {useRoute} from "vue-router"; import {useRoute} from "vue-router";
const {body} = document const {body} = document
const WIDTH = 992 const WIDTH = 992
@@ -38,14 +39,7 @@ export default defineComponent({
setup() { setup() {
const store = useStore() const store = useStore()
const device = computed(() => { const sidebar = computed(() => store.state.app.sidebar)
return store.state.app.device
})
const sidebar = computed(() => {
return store.state.app.sidebar
})
const isMobile = () => { const isMobile = () => {
const rect = body.getBoundingClientRect() const rect = body.getBoundingClientRect()
return rect.width - 1 < WIDTH return rect.width - 1 < WIDTH
@@ -81,7 +75,6 @@ export default defineComponent({
} }
}) })
const state = reactive({ const state = reactive({
handleClickOutside: () => { handleClickOutside: () => {
store.dispatch('app/closeSideBar', false) store.dispatch('app/closeSideBar', false)
@@ -90,10 +83,10 @@ export default defineComponent({
const classObj = computed(() => { const classObj = computed(() => {
return { return {
hideSidebar: !sidebar.opened, hideSidebar: !store.state.app.sidebar.opened,
openSidebar: sidebar.opened, openSidebar: store.state.app.sidebar.opened,
withoutAnimation: sidebar.withoutAnimation, withoutAnimation: store.state.app.sidebar.withoutAnimation,
mobile: device === 'mobile' mobile: store.state.app.device === 'mobile'
} }
}) })
const showSettings = computed(() => { const showSettings = computed(() => {
@@ -124,10 +117,10 @@ export default defineComponent({
return { return {
classObj, classObj,
sidebar,
showSettings, showSettings,
needTagsView, needTagsView,
fixedHeader, fixedHeader,
sidebar,
...toRefs(state) ...toRefs(state)
} }
} }

View File

@@ -2,13 +2,12 @@ import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'
import Layout from '@/layout/index.vue' import Layout from '@/layout/index.vue'
// 参数配置: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html // 参数说明: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
export const constantRoutes: Array<RouteRecordRaw> = [ export const constantRoutes: Array<RouteRecordRaw> = [
{ {
path: '/redirect', path: '/redirect',
component: Layout, component: Layout,
hidden: true, meta: {hidden: true},
children: [ children: [
{ {
path: '/redirect/:path(.*)', path: '/redirect/:path(.*)',
@@ -19,22 +18,23 @@ export const constantRoutes: Array<RouteRecordRaw> = [
{ {
path: '/login', path: '/login',
component: () => import('@/views/login/index.vue'), component: () => import('@/views/login/index.vue'),
hidden: true meta: {hidden: true}
}, },
{ {
path: '/404', path: '/404',
component: () => import('@/views/error-page/404.vue'), component: () => import('@/views/error-page/404.vue'),
hidden: true meta: {hidden: true}
}, },
{ {
path: '/401', path: '/401',
component: () => import('@/views/error-page/401.vue'), component: () => import('@/views/error-page/401.vue'),
hidden: true meta:{hidden: true}
}, },
{ {
path: '/', path: '/',
component: Layout, component: Layout,
redirect: '/dashboard', redirect: '/dashboard',
meta:{hidden:true},
children: [ children: [
{ {
path: 'dashboard', path: 'dashboard',
@@ -46,7 +46,6 @@ export const constantRoutes: Array<RouteRecordRaw> = [
} }
] ]
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
routes: constantRoutes routes: constantRoutes

View File

@@ -3,7 +3,7 @@ import {createStore,useStore as baseUseStore ,Store} from 'vuex'
import {RootStateTypes} from "@store/interface"; import {RootStateTypes} from "@store/interface";
// Vite 使用特殊的 import.meta.glob 函数从文件系统导入多个模块 // Vite 使用特殊的 import.meta.glob 函数从文件系统导入多个模块
// see https://cn.vitejs.dev/guide/features.html#glob-import // @see https://cn.vitejs.dev/guide/features.html#glob-import
const moduleFiles = import.meta.globEager('./modules/*.ts') const moduleFiles = import.meta.globEager('./modules/*.ts')
const paths:string[]=[] const paths:string[]=[]

View File

@@ -9,7 +9,6 @@ export interface UserState {
perms: string[] perms: string[]
} }
export interface AppState { export interface AppState {
device: string, device: string,
sidebar: { sidebar: {
@@ -26,7 +25,6 @@ export interface SettingState {
sidebarLogo: boolean sidebarLogo: boolean
} }
export interface PermissionState{ export interface PermissionState{
routes:RouteRecordRaw[] routes:RouteRecordRaw[]
addRoutes: RouteRecordRaw[] addRoutes: RouteRecordRaw[]
@@ -41,12 +39,11 @@ export interface TagsViewState{
cachedViews: (string|undefined)[] cachedViews: (string|undefined)[]
} }
// 顶级类型声明 // 顶级类型声明
export interface RootStateTypes { export interface RootStateTypes {
user: UserState, user: UserState,
app: AppState, app: AppState,
setting: SettingState, settings: SettingState,
permission:PermissionState, permission:PermissionState,
tagsView:TagsViewState tagsView:TagsViewState
} }

View File

@@ -14,6 +14,7 @@ const appModule: Module<AppState, RootStateTypes> = {
mutations: { mutations: {
TOGGLE_SIDEBAR: state => { TOGGLE_SIDEBAR: state => {
state.sidebar.opened = !state.sidebar.opened state.sidebar.opened = !state.sidebar.opened
console.log('state.sidebar.opened',state.sidebar.opened)
state.sidebar.withoutAnimation = false state.sidebar.withoutAnimation = false
if (state.sidebar.opened) { if (state.sidebar.opened) {
Local.set('sidebarStatus', 1) Local.set('sidebarStatus', 1)
@@ -27,6 +28,7 @@ const appModule: Module<AppState, RootStateTypes> = {
state.sidebar.withoutAnimation = withoutAnimation state.sidebar.withoutAnimation = withoutAnimation
}, },
TOGGLE_DEVICE: (state, device) => { TOGGLE_DEVICE: (state, device) => {
console.log('TOGGLE_DEVICE',device)
state.device = device state.device = device
} }
}, },
@@ -37,7 +39,7 @@ const appModule: Module<AppState, RootStateTypes> = {
closeSideBar({commit}, {withoutAnimation}) { closeSideBar({commit}, {withoutAnimation}) {
commit('CLOSE_SIDEBAR', withoutAnimation) commit('CLOSE_SIDEBAR', withoutAnimation)
}, },
toggleDevice({commit}, device) { async toggleDevice({commit}, device) {
commit('TOGGLE_DEVICE', device) commit('TOGGLE_DEVICE', device)
} }
} }

9
src/styles/variables.scss.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
export interface IScssVariables {
menuBg: string
menuText: string
menuActiveText: string
}
export const variables: IScssVariables
export default variables

View File

@@ -0,0 +1,13 @@
<template>
</template>
<script>
export default {
name: "index"
}
</script>
<style scoped>
</style>

View File

@@ -12,6 +12,7 @@ export default ({command, mode}: ConfigEnv): UserConfig => {
return ( return (
{ {
plugins: [ plugins: [
vue(), vue(),
viteSvgIcons({ viteSvgIcons({
@@ -35,6 +36,7 @@ export default ({command, mode}: ConfigEnv): UserConfig => {
} }
} }
}, },
resolve: { resolve: {
// Vite2设置别名路径方式一 // Vite2设置别名路径方式一
// alias:{ // alias:{