fix sidebar not expand when load
This commit is contained in:
@ -1,18 +1,22 @@
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { onMounted, ref, watch } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { convertRouteToMenu } from '@/utils/route'
|
||||
import { routes } from '@/router'
|
||||
import type { MenuItemModel } from '@/utils/interfaces'
|
||||
import { splitRoutePath } from '@/utils/texts'
|
||||
|
||||
export const useMenuStore = defineStore('menu', () => {
|
||||
const route = useRoute()
|
||||
const navigation = ref<MenuItemModel[]>([])
|
||||
const sidebarOpen = ref(false)
|
||||
const sidebarShowed = ref(true)
|
||||
const router = useRouter()
|
||||
const menuSelected = ref(route.fullPath)
|
||||
const toggleSidebar = () => (sidebarOpen.value = !sidebarOpen.value)
|
||||
const toggleSidebarMenu = (path: string, newExpanded: boolean): void => {
|
||||
console.log('expanded', path);
|
||||
|
||||
const toggleItemExpanded = (items: MenuItemModel[]): void => {
|
||||
for (const item of items) {
|
||||
if (item.path === path) {
|
||||
@ -37,6 +41,25 @@ export const useMenuStore = defineStore('menu', () => {
|
||||
onMounted(() => {
|
||||
const menuData = convertRouteToMenu(routes.at(0)?.children || [])
|
||||
navigation.value = menuData
|
||||
menuSelected.value = router.currentRoute.value.fullPath
|
||||
|
||||
if (menuSelected.value !== '/' && menuSelected.value !== '/home' && menuSelected.value.includes('/home')) {
|
||||
const result = splitRoutePath(menuSelected.value)
|
||||
|
||||
for (const route of result) {
|
||||
if (route !== '/home') {
|
||||
toggleSidebarMenu(route, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
watch(router, () => {
|
||||
if (router.currentRoute.value.fullPath === '/' || router.currentRoute.value.fullPath === '/home') {
|
||||
for (const item of navigation.value) {
|
||||
item.expanded = false
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
|
Reference in New Issue
Block a user