Skip to content

面包屑切换同步更新路由 #4

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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,389 changes: 1,389 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/components/SvgIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</template>

<script setup>
import { defineProps, computed } from 'vue'
import { computed } from 'vue'

const props = defineProps({
name: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/components/navbar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default {
})

// 切换菜单状态
const changeCollapse = (value) => store.commit('getCollapse', value)
const changeCollapse = (value) => store.commit('setCollapse', value)

watch(
() => store.state.activeMenu,
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/components/setting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const _changeSetting = (params) => {
settingThemes({ type, value })
if (type === 'navbarType') {
data.drawer = false
store.commit('getNavbarType', value)
store.commit('setNavbarType', value)
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/components/layout/components/sidebar/SidebarItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<script>
import { setTabs, getTabs } from 'utils/storage.js'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
props: {
Expand All @@ -24,6 +25,7 @@ export default {
},
setup() {
const store = useStore()
const router = useRouter()
// 点击菜单回调
const handleMenu = (obj) => {
const { menuId, menuName } = obj
Expand All @@ -38,10 +40,12 @@ export default {
tabs.push({
id: menuId,
name: menuName,
path: obj.path,
active: true
})
}
store.commit('getActiveMenu', menuId)
router.push({path: obj.path})
store.commit('setActiveMenu', menuId)
setTabs(tabs, menuId)
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/layout/components/sidebar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export default {
menuName: '导航一',
path: '',
children: [
{ menuId: '111-1', menuName: '选项1', path: '', children: [] },
{ menuId: '111-2', menuName: '选项2', path: '', children: [] },
{ menuId: '111-3', menuName: '选项3', path: '', children: [] },
{ menuId: '111-1', menuName: '选项1', path: '/home', children: [] },
{ menuId: '111-2', menuName: '选项2', path: '/test', children: [] },
{ menuId: '111-3', menuName: '选项3', path: '/', children: [] },
{
menuId: '111-4',
menuName: '这是选项4哦',
Expand Down
36 changes: 32 additions & 4 deletions src/components/layout/components/tabs/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
<script>
import { reactive, toRefs, ref, watch } from 'vue'
import { ArrowDown, Close, Download, DocumentRemove, DocumentDelete } from '@element-plus/icons'
import { setTabs, getTabs } from 'utils/storage.js'
import { setTabs, getTabs, getBreadcrumb } from 'utils/storage.js'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default {
components: {
Expand All @@ -75,6 +76,7 @@ export default {
},
setup() {
const store = useStore()
const router = useRouter()
const data = reactive({
left: 0,
tabs: getTabs(),
Expand Down Expand Up @@ -117,9 +119,11 @@ export default {

// 点击tab
const handleTag = (obj) => {
store.commit('getActiveMenu', obj.props.name)
store.commit('setActiveMenu', obj.props.name)
setTabs(data.tabs, obj.props.name)
console.log(obj.props.name)
judgeTabs()
router.push({path: getCurrentPath(obj.props.name)})
}

// 关闭tab
Expand All @@ -137,6 +141,7 @@ export default {
} else {
data.tabs.splice(idx, 1)
setTabs(data.tabs)
gotoCurrentPath()
judgeTabs()
}
}
Expand All @@ -153,21 +158,44 @@ export default {
currentIdx = currentIdx > 0 ? currentIdx - 1 : currentIdx + 1
menuId = tabs[currentIdx]?.id ?? ''
tabs = tabs.filter((item) => !item.active)
store.commit('getActiveMenu', menuId)
store.commit('setActiveMenu', menuId)
} else if (type === 'other') {
tabs = tabs.filter((item) => item.active)
data.tabs = tabs
} else if (type === 'all') {
tabs = tabs.filter((item) => item.unCloseable)
store.commit('getActiveMenu', '')
store.commit('setActiveMenu', '')
} else {
let len = tabs.length - 1
type === 'left' ? tabs.splice(0, currentIdx) : tabs.splice(currentIdx + 1, len - currentIdx)
}
setTabs(tabs, menuId)
gotoCurrentPath()
judgeTabs()
}

// 获取当前路由路径
const getCurrentPath = (id) => {
const tabs = getTabs()
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].id === id) {
return tabs[i].path
}
}
return undefined
}

// 跳转到当前路由
const gotoCurrentPath = () => {
const tabs = getTabs()
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].active) {
router.push({path: tabs[i].path})
return
}
}
}

// 判断是否可关闭导航栏
const judgeTabs = () => {
let disabledCurrent = true
Expand Down
3 changes: 2 additions & 1 deletion src/components/layout/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<template>
<layout>
<template #home>
<home></home>
<!-- <home></home> -->
<router-view />
</template>
</layout>
</template>
Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'element-plus/lib/theme-chalk/index.css'
import 'assets/css/layout.scss'
import 'assets/css/custom.scss'
import SvgIcon from 'components/SvgIcon.vue'
import './permission.js'

let app = createApp(App)
app.use(store)
Expand Down
23 changes: 23 additions & 0 deletions src/pages/login/Login.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
* Created Date: 2022-02-18 02:59:48
* Author: Virgil-N
* Description:
* -----
* Last Modified: 2022-02-18 03:32:19
* Modified By: Virgil-N (lieut9011@126.com)
* -----
* Copyright (c) 2019 - 2022 ⚐
* Virgil-N will save your soul!
* -----
-->

<template>
<p>login</p>
</template>

<script>
export default {
name: 'Login'
}
</script>
<style lang="scss" scoped></style>
20 changes: 20 additions & 0 deletions src/permission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Created Date: 2022-02-24 02:58:32
* Author: Virgil-N
* Description:
* -----
* Last Modified: 2022-02-24 03:04:24
* Modified By: Virgil-N (lieut9011@126.com)
* -----
* Copyright (c) 2019 - 2022 ⚐
* Virgil-N will save your soul!
* -----
*/

import router from './router'
import store from './store'

router.beforeEach(async (to, from, next) => {
console.log('router info: ', to, from)
next()
})
15 changes: 14 additions & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ const routes = [
{
path: 'home',
name: 'Home',
component: () => import('@/views/Home.vue')
component: () => import('@/views/Home.vue'),
meta: { requiresAuth: true }
},
{
path: 'test',
name: 'Test',
component: () => import('@/views/test/Test.vue'),
meta: { requiresAuth: true }
}
]
},
{
path: '/login',
name:'Login',
component: () => import('@/pages/login/Login.vue'),
meta: { requiresAuth: false }
}
]

Expand Down
12 changes: 6 additions & 6 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ export default createStore({
activeMenu: ''
},
mutations: {
// 获取导航栏类型
getNavbarType(state, data) {
// 设置导航栏类型
setNavbarType(state, data) {
state.navbarType = data
},

// 获取菜单折叠
getCollapse(state, data) {
// 设置菜单折叠
setCollapse(state, data) {
state.isCollapse = data
},

// 获取当前选中菜单
getActiveMenu(state, data) {
// 设置当前选中菜单
setActiveMenu(state, data) {
state.activeMenu = data
}
},
Expand Down
24 changes: 24 additions & 0 deletions src/views/test/Test.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
* Created Date: 2022-02-18 04:24:37
* Author: Virgil-N
* Description:
* -----
* Last Modified: 2022-02-18 04:34:25
* Modified By: Virgil-N (lieut9011@126.com)
* -----
* Copyright (c) 2019 - 2022 ⚐
* Virgil-N will save your soul!
* -----
-->

<template>
<p>test</p>
</template>

<script>
export default {
name: 'Test',
setup() {}
}
</script>
<style lang="scss" scoped></style>