Skip to content

Commit

Permalink
fix(layouts): 🐛 [layouts] 修复面包屑问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jsxiaosi committed Nov 13, 2022
1 parent daccd8d commit f252f50
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import qs from 'qs';
import { removeClass, toggleClass } from '@/utils/operate';
import type { MultiTabsType } from '@/store/types';
import { usePermissionStoreHook } from '@/store/modules/permission';
import { findRouteByPath } from '@/router/utils';
import { isUrl } from '@/utils/is';
import type { AppRouteRecordRaw } from '#/route';

export const useTabsChange = (multiTabs: Ref<MultiTabsType[]>) => {
const route = useRoute();
Expand Down Expand Up @@ -89,24 +86,5 @@ export const useTabsChange = (multiTabs: Ref<MultiTabsType[]>) => {
}, 600);
}

const selectMenu = (path: string) => {
const routerinfo = (router.options.routes.find((i) => i.path === '/') ||
[]) as AppRouteRecordRaw;
const findRoute = findRouteByPath(path, routerinfo.children || []);
if (findRoute) {
if (findRoute.redirect && findRoute.children && findRoute.children.length) {
selectMenu(findRoute.children[0].path);
return;
}

if (isUrl(findRoute.path)) return;
addRouteTabs(route);
}
};

// const logout = () => {
// usePermissionStoreHook().handleRemoveMultiTabs();
// };

return { setTabPaneKey, addRouteTabs, onFresh, closeTabsRoute, removeTab, selectMenu };
return { setTabPaneKey, addRouteTabs, onFresh, closeTabsRoute, removeTab };
};
13 changes: 8 additions & 5 deletions src/layouts/pageLayouts/components/AppTabs/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { MultiTabsType } from '@/store/types';
import SvgIcon from '@/components/SvgIcon/index.vue';
import { useRootSetting } from '@/hooks/setting/useRootSetting';
import { emitter } from '@/utils/mitt';
const { appConfig, setAppConfigMode } = useRootSetting();
Expand All @@ -19,21 +20,23 @@
const { visible, rightClickTags, rightViewStyle, contextmenu, rightViewChange } =
useTabsView(multiTabs);
const { setTabPaneKey, selectMenu, onFresh, removeTab } = useTabsChange(multiTabs);
const { setTabPaneKey, addRouteTabs, onFresh, removeTab } = useTabsChange(multiTabs);
const editableTabsValue = ref(setTabPaneKey(route));
watch(
() => [route.path, route.query],
() => [route.path],
async () => {
await selectMenu(route.path);
contextmenu(route);
setTimeout(() => (editableTabsValue.value = setTabPaneKey(route)));
editableTabsValue.value = setTabPaneKey(route);
},
);
onBeforeMount(() => {
selectMenu(route.path);
emitter.on('siteBarChange', ({ routeRaw }) => {
addRouteTabs(routeRaw as unknown as MultiTabsType);
});
addRouteTabs(route);
contextmenu(route);
});
Expand Down
14 changes: 10 additions & 4 deletions src/layouts/pageLayouts/components/Breadcrumb/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, watch } from 'vue';
import { onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { isEqual } from 'lodash-es';
import AppFold from '../AppFold/index.vue';
Expand All @@ -23,7 +23,10 @@
// 解析路由匹配的数组
const getBreadcrumb = () => {
const matched: AppRouteRecordRaw[] = [];
const parentRoutes = getParentPaths(router.currentRoute.value.path || '', routes || []);
const parentRoutes = getParentPaths(
router.currentRoute.value.matched[1].path || '',
routes || [],
);
// 获取每个父级路径对应的路由信息
parentRoutes.forEach((path) => {
if (path !== '/') {
Expand All @@ -36,7 +39,9 @@
itemQuery = JSON.parse(JSON.stringify(item.query));
}
if (matched.find((i) => i.path === item.path)) return false;
return route.name === item.name && isEqual(route.query, itemQuery);
return (
route.name === item.name && isEqual(route.query, itemQuery) && route.path === item.path
);
});
if (item) matched.push(item as unknown as AppRouteRecordRaw);
levelList.value = matched.filter(
Expand All @@ -60,7 +65,8 @@
router.push(pathCompile(path));
};
// 首次调用
getBreadcrumb();
onMounted(getBreadcrumb);
// 监控route的变化,避免组件复用信息同步问题
watch(route, getBreadcrumb);
Expand Down
4 changes: 4 additions & 0 deletions src/layouts/pageLayouts/components/Sidebar/MinSidebar.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { computed } from 'vue';
import { useRoute } from 'vue-router';
import { useNavSideBar } from '../../hooks/useNavSideBar';
import Item from './Item.vue';
import AppLink from './Link.vue';
import { usePermissionStoreHook } from '@/store/modules/permission';
Expand All @@ -9,6 +10,8 @@
const route = useRoute();
const { selectMenu } = useNavSideBar();
const resolvePath = (routeRaw: AppRouteRecordRaw): string => {
let path = routeRaw.path;
if (routeRaw.children && routeRaw.children.length && !routeRaw.children[0].hidden) {
Expand Down Expand Up @@ -36,6 +39,7 @@
:default-active="activeMenyu"
class="horizontal-header-menu"
mode="horizontal"
@select="(indexPath: string) => selectMenu(indexPath)"
>
<AppLink
v-for="menusRoute in usePermissionStoreHook().wholeMenus"
Expand Down
4 changes: 4 additions & 0 deletions src/layouts/pageLayouts/components/Sidebar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { PropType } from 'vue';
import { computed, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useNavSideBar } from '../../hooks/useNavSideBar';
import SidebarItem from './SidebarItem.vue';
import { usePermissionStoreHook } from '@/store/modules/permission';
import type { AppRouteRecordRaw } from '#/route';
Expand All @@ -15,6 +16,8 @@
},
});
const { selectMenu } = useNavSideBar();
const route = useRoute();
const { appConfig } = useRootSetting();
let subMenuData = ref<AppRouteRecordRaw[]>(usePermissionStoreHook().wholeMenus);
Expand Down Expand Up @@ -63,6 +66,7 @@
:unique-opened="true"
:collapse="appConfig.sidebarMode === 'horizontal' ? false : appConfig.collapseMenu"
:mode="mode"
@select="(indexPath: string) => selectMenu(indexPath)"
>
<SidebarItem
v-for="menuRoute in menuData"
Expand Down
32 changes: 32 additions & 0 deletions src/layouts/pageLayouts/hooks/useNavSideBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useRouter } from 'vue-router';
import type { AppRouteRecordRaw } from '#/route';
import { findRouteByPath } from '@/router/utils';
import { usePermissionStoreHook } from '@/store/modules/permission';
import { isUrl } from '@/utils/is';
import { emitter } from '@/utils/mitt';

export const useNavSideBar = () => {
const router = (useRouter().options.routes.find((i) => i.path === '/') ||
[]) as AppRouteRecordRaw;

const selectMenu = (path: string) => {
const findRoute = findRouteByPath(path, router.children || []);
if (findRoute) {
if (findRoute.redirect && findRoute.children && findRoute.children.length) {
selectMenu(findRoute.children[0].path);
return;
}

if (isUrl(findRoute.path)) return;
emitter.emit('siteBarChange', {
routeRaw: findRoute,
});
}
};

const logout = () => {
usePermissionStoreHook().handleRemoveMultiTabs();
};

return { selectMenu, logout };
};
22 changes: 5 additions & 17 deletions src/views/details-page/datails-info/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { onBeforeMount, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { usePermissionStoreHook } from '@/store/modules/permission';
import { useRoute } from 'vue-router';
import { useDatailsInfo } from '../hooks/useDatailsInfo';
defineOptions({
name: 'RtDetailsInfo',
Expand All @@ -10,24 +10,12 @@
const route = useRoute();
const { query } = route;
const router = useRouter();
const value = ref('');
const toPath = (item: string) => {
const query = { id: `${item}` };
usePermissionStoreHook().handleMultiTabs('add', {
path: `/details_page/details_info`,
name: `RtDetailsInfo`,
query,
meta: {
title: { 'zh-ch': `详情页-${item}`, en: `pageDatails-${item}` },
},
});
router.push({ name: 'RtDetailsInfo', query });
};
const { toDatailsInfo } = useDatailsInfo();
onBeforeMount(() => {
toPath(query.id as string);
toDatailsInfo(`${query.id}`, 'query');
});
</script>

Expand Down
23 changes: 5 additions & 18 deletions src/views/details-page/datails-params/index.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
<script setup lang="ts">
import { onBeforeMount } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { usePermissionStoreHook } from '@/store/modules/permission';
import { useRoute } from 'vue-router';
import { useDatailsInfo } from '../hooks/useDatailsInfo';
defineOptions({
name: 'RtDetailsInfo',
});
const route = useRoute();
const { path, params } = route;
const { params } = route;
const { toDatailsInfo } = useDatailsInfo();
const router = useRouter();
const toPath = (item: string) => {
const params = { id: `${item}` };
usePermissionStoreHook().handleMultiTabs('add', {
path,
name: `RtDetailsParams`,
params,
meta: {
title: { 'zh-ch': `详情页-params-${item}`, en: `pageDatails-${item}` },
},
});
router.push({ name: 'RtDetailsParams', params });
};
onBeforeMount(() => {
toPath(params.id as string);
toDatailsInfo(`${params.id}`, 'params');
});
</script>

Expand Down
32 changes: 32 additions & 0 deletions src/views/details-page/hooks/useDatailsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useRouter } from 'vue-router';
import { usePermissionStoreHook } from '@/store/modules/permission';

export function useDatailsInfo() {
const router = useRouter();

function toDatailsInfo(params: string, model: string) {
if (model === 'query') {
usePermissionStoreHook().handleMultiTabs('add', {
path: `/details_page/details_info`,
name: `RtDetailsInfo`,
query: { id: `${params}` },
meta: {
title: { 'zh-ch': `详情页-${params}`, en: `pageDatails-${params}` },
},
});
router.push({ name: 'RtDetailsInfo', query: { id: `${params}` } });
} else {
usePermissionStoreHook().handleMultiTabs('add', {
path: `/details_page/details_params/${params}`,
name: `RtDetailsParams`,
params: { id: `${params}` },
meta: {
title: { 'zh-ch': `详情页-params-${params}`, en: `pageDatails-${params}` },
},
});
router.push({ name: 'RtDetailsParams', params: { id: `${params}` } });
}
}

return { toDatailsInfo };
}
12 changes: 3 additions & 9 deletions src/views/details-page/index.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
<script setup lang="ts">
import { useRouter } from 'vue-router';
import { useDatailsInfo } from './hooks/useDatailsInfo';
// import { usePermissionStoreHook } from '@/store/modules/permission';
const router = useRouter();
const { toDatailsInfo } = useDatailsInfo();
const toPath = (item: number, type = 'query') => {
if (type === 'params') {
const params = { id: `${item}` };
router.push({ name: 'RtDetailsParams', params });
} else {
const query = { id: `${item}` };
router.push({ name: 'RtDetailsInfo', query });
}
toDatailsInfo(`${item}`, type);
};
</script>

Expand Down

0 comments on commit f252f50

Please sign in to comment.