|
|
@@ -2,7 +2,7 @@ import { defineStore } from 'pinia'
|
|
|
import { ConstantStore } from '@/enum/store'
|
|
|
import { ref, computed, onMounted, watch, reactive, watchEffect } from 'vue'
|
|
|
import RootRouter from '@/router'
|
|
|
-import { useLocalStorageState } from 'vue-hooks-plus'
|
|
|
+import { useSessionStorageState } from 'vue-hooks-plus'
|
|
|
import { AppRouter as AppRouterEnum, Emitter } from '@/enum'
|
|
|
import { useEmitter } from '@/hooks/index'
|
|
|
import { useRoute } from 'vue-router'
|
|
|
@@ -31,6 +31,10 @@ const initAppRouterState: ROUTER.RouterRecords = {
|
|
|
export const useAppRouter = defineStore(ConstantStore.ROUTER, () => {
|
|
|
const appRouter = reactive<ROUTER.RouterRecords>(initAppRouterState)
|
|
|
|
|
|
+ const [currentRoute, setCurrentRoute] = useSessionStorageState<string>('currentRoute', {
|
|
|
+ defaultValue: '/'
|
|
|
+ })
|
|
|
+
|
|
|
// const [appRouterState, setAppRouterState] = useLocalStorageState<ROUTER.RouterRecords>(AppRouterEnum.ROUTER, {
|
|
|
// defaultValue: initAppRouterState
|
|
|
// })
|
|
|
@@ -40,6 +44,8 @@ export const useAppRouter = defineStore(ConstantStore.ROUTER, () => {
|
|
|
const siderRoutes = computed(() => RootRouter.getRoutes().find(item => item.path === appRouter.navbar!.selectPath)?.children)
|
|
|
|
|
|
const initAppRouter = () => {
|
|
|
+ console.log('initAppRouter')
|
|
|
+
|
|
|
appRouter.navbar.route = RootRouter.options.routes.map(route => {
|
|
|
return {
|
|
|
path: route.path,
|
|
|
@@ -47,14 +53,20 @@ export const useAppRouter = defineStore(ConstantStore.ROUTER, () => {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- appRouter.navbar.selectPath = RootRouter.options.routes[0].path
|
|
|
+ // appRouter.navbar.selectPath = RootRouter.options.routes[0].path
|
|
|
+ appRouter.navbar.selectPath = currentRoute.value!
|
|
|
|
|
|
appRouter.sider.route = siderRoutes.value!
|
|
|
+
|
|
|
+ appRouter.sider.selectPath = currentRoute.value!
|
|
|
}
|
|
|
|
|
|
- const changeNavbar = (path: string) => {
|
|
|
+ const changeNavbar = (path: string, key: 'init' | '' = '') => {
|
|
|
+ console.log('changeNavbar:', path, currentRoute.value)
|
|
|
+
|
|
|
RootRouter.push(path)
|
|
|
appRouter.navbar.selectPath = path
|
|
|
+ setCurrentRoute(key === 'init' ? currentRoute.value! : siderRoutes.value![0].path)
|
|
|
}
|
|
|
|
|
|
watch(
|
|
|
@@ -63,8 +75,6 @@ export const useAppRouter = defineStore(ConstantStore.ROUTER, () => {
|
|
|
)
|
|
|
|
|
|
const changeSiderRoute = () => {
|
|
|
- console.log(' appRouter.navbar!.selectPath:', appRouter.navbar!.selectPath)
|
|
|
-
|
|
|
appRouter.sider = {
|
|
|
route: RootRouter.getRoutes().find(item => item.path === appRouter.navbar!.selectPath)?.children as ROUTER.RoutesProps[],
|
|
|
selectPath: RootRouter.currentRoute.value.path,
|
|
|
@@ -76,9 +86,22 @@ export const useAppRouter = defineStore(ConstantStore.ROUTER, () => {
|
|
|
if (appRouter.navbar.route.length === 0) {
|
|
|
initAppRouter()
|
|
|
}
|
|
|
+
|
|
|
+ const cancheCurrentRoute = () => {
|
|
|
+ appRouter.sider.selectPath = currentRoute.value!
|
|
|
+ // RootRouter.push(currentRoute.value!)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 通过session store 保存当前的路由 防止刷新丢失路由
|
|
|
+ onMounted(() => {
|
|
|
+ cancheCurrentRoute()
|
|
|
+ })
|
|
|
+
|
|
|
return {
|
|
|
router: appRouter,
|
|
|
+ currentRoute,
|
|
|
changeNavbar,
|
|
|
- changeSiderRoute
|
|
|
+ changeSiderRoute,
|
|
|
+ setCurrentRoute
|
|
|
}
|
|
|
})
|