fixed: 首次进入没有名称.

This commit is contained in:
lijiahang
2023-08-02 17:54:12 +08:00
parent 7915198be4
commit 598e33b0b5
9 changed files with 60 additions and 67 deletions

View File

@@ -78,7 +78,6 @@
* 监听路由 设置打开的 key * 监听路由 设置打开的 key
*/ */
listenerRouteChange((newRoute) => { listenerRouteChange((newRoute) => {
// TODO
const { activeMenu, hideInMenu } = newRoute.meta; const { activeMenu, hideInMenu } = newRoute.meta;
if (!hideInMenu || activeMenu) { if (!hideInMenu || activeMenu) {
const menuOpenKeys = findMenuOpenKeys( const menuOpenKeys = findMenuOpenKeys(

View File

@@ -21,6 +21,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onUnmounted, ref, watch } from 'vue'; import { computed, onUnmounted, ref, watch } from 'vue';
import type { RouteLocationNormalized } from 'vue-router'; import type { RouteLocationNormalized } from 'vue-router';
import { routerToTag } from '@/router/constants';
import { listenerRouteChange, removeRouteListener, } from '@/utils/route-listener'; import { listenerRouteChange, removeRouteListener, } from '@/utils/route-listener';
import { useAppStore, useTabBarStore } from '@/store'; import { useAppStore, useTabBarStore } from '@/store';
import TabItem from './tab-item.vue'; import TabItem from './tab-item.vue';
@@ -46,16 +47,13 @@
/** /**
* 监听路由变化 * 监听路由变化
*/ */
// TODO
listenerRouteChange((route: RouteLocationNormalized) => { listenerRouteChange((route: RouteLocationNormalized) => {
console.log(route);
console.log(!route.meta.noAffix);
if ( if (
!route.meta.noAffix && // todo 改一下 !route.meta.noAffix &&
!tagList.value.some((tag) => tag.fullPath === route.fullPath) !tagList.value.some((tag) => tag.fullPath === route.fullPath)
) { ) {
console.log('updateTabList', route); // 固定并且没有此 tab 则添加
tabBarStore.updateTabList(route); tabBarStore.addTab(routerToTag(route), route.meta?.ignoreCache as unknown as any);
} }
}, true); }, true);

View File

@@ -17,33 +17,33 @@
</span> </span>
</span> </span>
<template #content> <template #content>
<a-doption :disabled="disabledReload" :value="Eaction.reload"> <a-doption :disabled="disabledReload" :value="Option.reload">
<icon-refresh /> <icon-refresh />
<span>重新加载</span> <span>重新加载</span>
</a-doption> </a-doption>
<a-doption <a-doption
class="sperate-line" class="group-line"
:disabled="disabledCurrent" :disabled="disabledCurrent"
:value="Eaction.current"> :value="Option.current">
<icon-close /> <icon-close />
<span>关闭当前标签页</span> <span>关闭当前标签页</span>
</a-doption> </a-doption>
<a-doption :disabled="disabledLeft" :value="Eaction.left"> <a-doption :disabled="disabledLeft" :value="Option.left">
<icon-to-left /> <icon-to-left />
<span>关闭左侧标签页</span> <span>关闭左侧标签页</span>
</a-doption> </a-doption>
<a-doption <a-doption
class="sperate-line" class="group-line"
:disabled="disabledRight" :disabled="disabledRight"
:value="Eaction.right"> :value="Option.right">
<icon-to-right /> <icon-to-right />
<span>关闭右侧标签页</span> <span>关闭右侧标签页</span>
</a-doption> </a-doption>
<a-doption :value="Eaction.others"> <a-doption :value="Option.others">
<icon-swap /> <icon-swap />
<span>关闭其它标签页</span> <span>关闭其它标签页</span>
</a-doption> </a-doption>
<a-doption :value="Eaction.all"> <a-doption :value="Option.all">
<icon-folder-delete /> <icon-folder-delete />
<span>关闭全部标签页</span> <span>关闭全部标签页</span>
</a-doption> </a-doption>
@@ -58,8 +58,7 @@
import type { TagProps } from '@/store/modules/tab-bar/types'; import type { TagProps } from '@/store/modules/tab-bar/types';
import { DEFAULT_ROUTE_NAME, REDIRECT_ROUTE_NAME } from '@/router/constants'; import { DEFAULT_ROUTE_NAME, REDIRECT_ROUTE_NAME } from '@/router/constants';
// eslint-disable-next-line no-shadow enum Option {
enum Eaction {
reload = 'reload', reload = 'reload',
current = 'current', current = 'current',
left = 'left', left = 'left',
@@ -126,48 +125,46 @@
const actionSelect = async (value: any) => { const actionSelect = async (value: any) => {
const { itemData, index } = props; const { itemData, index } = props;
const copyTagList = [...tagList.value]; const copyTagList = [...tagList.value];
if (value === Eaction.current) { if (value === Option.current) {
// 关闭当前 // 关闭当前
tagClose(itemData, index); tagClose(itemData, index);
} else if (value === Eaction.left) { } else if (value === Option.left) {
// 关闭左侧 // 关闭左侧
const currentRouteIdx = findCurrentRouteIndex(); const currentRouteIdx = findCurrentRouteIndex();
copyTagList.splice(1, props.index - 1); copyTagList.splice(1, props.index - 1);
tabBarStore.freshTabList(copyTagList); tabBarStore.freshTabList(copyTagList);
if (currentRouteIdx < index) { if (currentRouteIdx < index) {
router.push({ name: itemData.name }); await router.push({ name: itemData.name });
} }
} else if (value === Eaction.right) { } else if (value === Option.right) {
// 关闭右侧 // 关闭右侧
const currentRouteIdx = findCurrentRouteIndex(); const currentRouteIdx = findCurrentRouteIndex();
copyTagList.splice(props.index + 1); copyTagList.splice(props.index + 1);
tabBarStore.freshTabList(copyTagList); tabBarStore.freshTabList(copyTagList);
if (currentRouteIdx > index) { if (currentRouteIdx > index) {
router.push({ name: itemData.name }); await router.push({ name: itemData.name });
} }
} else if (value === Eaction.others) { } else if (value === Option.others) {
// 关闭其他 // 关闭其他
const filterList = tagList.value.filter((el, idx) => { const filterList = tagList.value.filter((el, idx) => {
return idx === 0 || idx === props.index; return idx === 0 || idx === props.index;
}); });
tabBarStore.freshTabList(filterList); tabBarStore.freshTabList(filterList);
router.push({ name: itemData.name }); await router.push({ name: itemData.name });
} else if (value === Eaction.reload) { } else if (value === Option.reload) {
// 重新加载 // 重新加载
tabBarStore.deleteCache(itemData); tabBarStore.deleteCache(itemData);
await router.push({ await router.push({
name: REDIRECT_ROUTE_NAME, name: REDIRECT_ROUTE_NAME,
params: { params: { path: route.fullPath },
path: route.fullPath,
},
}); });
tabBarStore.addCache(itemData.name); tabBarStore.addCache(itemData.name);
} else { } else {
// 关闭全部 // 关闭全部
tabBarStore.resetTabList(); tabBarStore.resetTabList();
router.push({ name: DEFAULT_ROUTE_NAME }); await router.push({ name: DEFAULT_ROUTE_NAME });
} }
}; };
</script> </script>
@@ -206,7 +203,7 @@
} }
} }
.sperate-line { .group-line {
border-bottom: 1px solid var(--color-neutral-3); border-bottom: 1px solid var(--color-neutral-3);
} }
</style> </style>

View File

@@ -1,15 +1,28 @@
import { RouteLocationNormalized, RouteRecordRaw } from 'vue-router'; import { RouteLocationNormalized, RouteRecordNormalized, RouteRecordRaw } from 'vue-router';
import { useUserStore } from '@/store'; import { useAppStore, useUserStore } from '@/store';
import { WHITE_ROUTER_LIST } from '@/router/constants';
export default function usePermission() { export default function usePermission() {
const appStore = useAppStore();
const userStore = useUserStore(); const userStore = useUserStore();
return { return {
/** /**
* 是否可访问路由 * 是否可访问路由
*/ */
accessRouter(route: RouteLocationNormalized | RouteRecordRaw) { accessRouter(route: RouteLocationNormalized | RouteRecordRaw) {
// route.name // 检查路由是否存在于授权路由中
// TODO const menuConfig = [...appStore.appAsyncMenus, ...WHITE_ROUTER_LIST];
let exist = false;
while (menuConfig.length && !exist) {
const element = menuConfig.shift();
if (element?.name === route.name) exist = true;
if (element?.children) {
menuConfig.push(
...(element.children as unknown as RouteRecordNormalized[])
);
}
}
return exist;
}, },
/** /**

View File

@@ -35,13 +35,10 @@ export const DEFAULT_TAB = {
/** /**
* router 转 tag * router 转 tag
*/ */
// TODO 获取后端meta
export const routerToTag = (route: RouteLocationNormalized): TagProps => { export const routerToTag = (route: RouteLocationNormalized): TagProps => {
console.log(route);
// TODO 还是得需要 name 和 meta 的映射
const { name, meta, fullPath, query } = route; const { name, meta, fullPath, query } = route;
return { return {
title: meta.locale || 'me', title: meta.locale || '',
name: String(name), name: String(name),
fullPath, fullPath,
query, query,

View File

@@ -1,12 +1,12 @@
import type { Router, RouteRecordNormalized } from 'vue-router'; import type { Router } from 'vue-router';
import NProgress from 'nprogress'; import NProgress from 'nprogress';
import { useAppStore } from '@/store'; import { useAppStore } from '@/store';
import { NOT_FOUND_ROUTER_NAME, WHITE_ROUTER_LIST } from '../constants'; import { NOT_FOUND_ROUTER_NAME, WHITE_ROUTER_LIST } from '../constants';
import usePermission from '@/hooks/permission';
export default function setupPermissionGuard(router: Router) { export default function setupPermissionGuard(router: Router) {
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
const appStore = useAppStore(); const appStore = useAppStore();
// 未加载菜单 并且 不在白名单内 则加载菜单 // 未加载菜单 并且 不在白名单内 则加载菜单
if ( if (
!appStore.menuFetched && !appStore.menuFetched &&
@@ -15,25 +15,20 @@ export default function setupPermissionGuard(router: Router) {
// 加载菜单 // 加载菜单
await appStore.fetchMenuConfig(); await appStore.fetchMenuConfig();
} }
// 检查路由是否存在于授权路由中 // 检测是否可以访问
const menuConfig = [...appStore.appAsyncMenus, ...WHITE_ROUTER_LIST]; const permission = usePermission();
let exist = false; const access = permission.accessRouter(to);
while (menuConfig.length && !exist) { // 刚进入页面时 重定向的 meta 是空的
const element = menuConfig.shift(); if (access && to.meta.locale === undefined && appStore.menuFetched) {
if (element?.name === to.name) exist = true; to.meta = to.matched[to.matched.length - 1].meta;
if (element?.children) {
menuConfig.push(
...(element.children as unknown as RouteRecordNormalized[])
);
}
} }
if (!exist) {
// 页面不存在 if (access) {
next({ name: NOT_FOUND_ROUTER_NAME });
} else {
// 正常跳转 // 正常跳转
next(); next();
} else {
// 页面不存在
next({ name: NOT_FOUND_ROUTER_NAME });
} }
NProgress.done(); NProgress.done();
}); });

View File

@@ -15,9 +15,6 @@ const USER: AppRouteRecordRaw = {
path: '/user/userChild2', path: '/user/userChild2',
name: 'userChild2', name: 'userChild2',
component: () => import('@/views/user/child2/index.vue'), component: () => import('@/views/user/child2/index.vue'),
meta: {
noAffix: true
}
}, },
], ],
}; };

View File

@@ -1,6 +1,5 @@
import type { RouteLocationNormalized } from 'vue-router';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { DEFAULT_TAB, DEFAULT_ROUTE_NAME, routerToTag } from '@/router/constants'; import { DEFAULT_ROUTE_NAME, DEFAULT_TAB } from '@/router/constants';
import { isString } from '@/utils/is'; import { isString } from '@/utils/is';
import { TabBarState, TagProps } from './types'; import { TabBarState, TagProps } from './types';
@@ -23,10 +22,10 @@ const useTabBarStore = defineStore('tabBar', {
/** /**
* 添加 tab * 添加 tab
*/ */
updateTabList(route: RouteLocationNormalized) { addTab(tag: TagProps, ignoreCache: boolean) {
this.tagList.push(routerToTag(route)); this.tagList.push(tag);
if (!route.meta.ignoreCache) { if (!ignoreCache) {
this.cacheTabList.add(route.name as string); this.cacheTabList.add(tag.name as string);
} }
}, },

View File

@@ -9,9 +9,7 @@ let latestRoute: RouteLocationNormalized;
export function setRouteEmitter(to: RouteLocationNormalized) { export function setRouteEmitter(to: RouteLocationNormalized) {
emitter.emit(key, to); emitter.emit(key, to);
// TODO 这里寻找
latestRoute = to; latestRoute = to;
console.log('change', to);
} }
/** /**