admin-22.05.01:重构路由,解决部分No match found for location with path xxx

This commit is contained in:
lyt 2022-05-01 11:43:53 +08:00
parent faf372a8b9
commit 6eaad912f5
16 changed files with 272 additions and 186 deletions

View File

@ -16,7 +16,7 @@
- 🎯 优化 去掉开发环境 i18n 控制台警告,页面代码:[i18n/index.ts](https://gitee.com/lyt-top/vue-next-admin/blob/master/src/i18n/index.ts)
- 🎯 优化 `NextLoading.start()` 方法,防止第一次进入界面时出现短暂空白
- 🎯 优化 地址栏有参数退出登录,再次登录不跳之前界面问题 `src/layout/navBars/breadcrumb/user.vue`
- 🎯 优化 `SvgIcon` 组件,防止 `开启 Tagsview 图标` 时,`tagsView 右键菜单关闭` 报错问题
- 🎯 优化 `SvgIcon` 组件,防止 `开启 Tagsview 图标` 时,`tagsView 右键菜单关闭` 报错问题,工作流不可连线问题
- 🎯 优化 [wangEditor](https://www.wangeditor.com/) 更新到 v5[vue3 版本线上示例中 wangeditor 富文本编辑器 demo 实例,无法换行#I5565B](https://gitee.com/lyt-top/vue-next-admin/issues/I5565B),感谢@[jenchih](https://gitee.com/jenchih)
- 🎯 优化 [在关闭 tagview 时,高度刷新时会会变化,出现滚动条](https://gitee.com/lyt-top/vue-next-admin/issues/I55FHM),感谢[张松](https://gitee.com/zs310071113)
- 🎉 新增 [vuex](https://vuex.vuejs.org/) 替换成 [pinia](https://pinia.vuejs.org/getting-started.html)
@ -31,6 +31,7 @@
- 🐞 修复 `开启 Tagsview 图标` 时,`tagsView 右键菜单关闭` 报错问题
- 🐞 修复 `router.push` 路径找不到时报错问题,`404、401 界面` 已移入到 `main` 主布局里(之前全屏)
- 🐞 修复 [全局修改组件大小失效了](https://gitee.com/lyt-top/vue-next-admin/issues/I551RP),感谢[lg_boy](https://gitee.com/lg_boy)
- 🌈 重构 路由(`/src/router/index.ts`)解决 No match found for location with path "xxx"(前端控制,后端控制未解决) 问题
## 2.0.2

View File

@ -1,11 +1,9 @@
<template>
<div class="svg-icon-container">
<i v-if="isShowIconSvg" class="el-icon" :style="setIconSvgStyle">
<component :is="getIconName" />
</i>
<img v-else-if="isShowIconImg" :src="getIconName" :style="setIconImgStyle" />
<i v-else :class="getIconName" :style="setIconSvgStyle" />
</div>
<i v-if="isShowIconSvg" class="el-icon" :style="setIconSvgStyle">
<component :is="getIconName" />
</i>
<img v-else-if="isShowIconImg" :src="getIconName" :style="setIconImgStyle" />
<i v-else :class="getIconName" :style="setIconSvgStyle" />
</template>
<script lang="ts">
@ -46,7 +44,7 @@ export default defineComponent({
});
//
const setIconSvgStyle = computed(() => {
return `font-size: ${props.size}px;color: ${props.color};height: 100%;display: flex;align-items: center;`;
return `font-size: ${props.size}px;color: ${props.color};`;
});
//
const setIconImgStyle = computed(() => {
@ -62,9 +60,3 @@ export default defineComponent({
},
});
</script>
<style scoped lang="scss">
.svg-icon-container {
display: inline-block;
}
</style>

View File

@ -105,7 +105,6 @@ export default {
logOutConfirm: 'determine',
logOutCancel: 'cancel',
logOutExit: 'Exiting',
logOutSuccess: 'Exit successfully!',
},
tagsView: {
refresh: 'refresh',

View File

@ -105,7 +105,6 @@ export default {
logOutConfirm: '确定',
logOutCancel: '取消',
logOutExit: '退出中',
logOutSuccess: '安全退出成功!',
},
tagsView: {
refresh: '刷新',

View File

@ -105,7 +105,6 @@ export default {
logOutConfirm: '確定',
logOutCancel: '取消',
logOutExit: '退出中',
logOutSuccess: '安全退出成功!',
},
tagsView: {
refresh: '重繪',

View File

@ -82,7 +82,6 @@ import { useRouter } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import screenfull from 'screenfull';
import { useI18n } from 'vue-i18n';
import { resetRoute } from '/@/router/index';
import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
import { useThemeConfig } from '/@/stores/themeConfig';
@ -162,8 +161,7 @@ export default defineComponent({
})
.then(async () => {
Session.clear(); // /token
await resetRoute(); // /
ElMessage.success(t('message.user.logOutSuccess'));
// 使 reload resetRoute()
window.location.reload();
})
.catch(() => {});

View File

@ -1,15 +1,23 @@
import { RouteRecordRaw } from 'vue-router';
import { storeToRefs } from 'pinia';
import pinia from '/@/stores/index';
import { useUserInfo } from '/@/stores/userInfo';
import { useRequestOldRoutes } from '/@/stores/requestOldRoutes';
import { Session } from '/@/utils/storage';
import { NextLoading } from '/@/utils/loading';
import { setAddRoute, setFilterMenuAndCacheTagsViewRoutes } from '/@/router/index';
import { dynamicRoutes } from '/@/router/route';
import { dynamicRoutes, notFoundAndNoPower } from '/@/router/route';
import { formatTwoStageRoutes, formatFlatteningRoutes, router } from '/@/router/index';
import { useRoutesList } from '/@/stores/routesList';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
import { useMenuApi } from '/@/api/menu/index';
const menuApi = useMenuApi();
const layouModules: any = import.meta.glob('../layout/routerView/*.{vue,tsx}');
const viewsModules: any = import.meta.glob('../views/**/*.{vue,tsx}');
// 后端控制路由
/**
* .vue.tsx
* @method import.meta.glob
@ -21,7 +29,7 @@ const dynamicViewsModules: Record<string, Function> = Object.assign({}, { ...lay
*
* @method NextLoading loading
* @method useUserInfo().setUserInfos() pinia
* @method useRequestOldRoutes().setCacheKeepAlive() component使
* @method useRequestOldRoutes().setRequestOldRoutes() component使
* @method setAddRoute
* @method setFilterMenuAndCacheTagsViewRoutes vuex routesList
*/
@ -36,13 +44,59 @@ export async function initBackEndControlRoutes() {
// 获取路由菜单数据
const res = await getBackEndControlRoutes();
// 存储接口原始路由未处理component根据需求选择使用
useRequestOldRoutes().setCacheKeepAlive(JSON.parse(JSON.stringify(res.data)));
useRequestOldRoutes().setRequestOldRoutes(JSON.parse(JSON.stringify(res.data)));
// 处理路由component替换 dynamicRoutes/@/router/route第一个顶级 children 的路由
dynamicRoutes[0].children = await backEndComponent(res.data);
// 添加动态路由
await setAddRoute();
// 设置递归过滤有权限的路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
setFilterMenuAndCacheTagsViewRoutes();
// // 设置递归过滤有权限的路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
await setFilterMenuAndCacheTagsViewRoutes();
}
/**
* vuex routesList
* @description
* @description tagsView(isHide)
*/
export function setFilterMenuAndCacheTagsViewRoutes() {
const storesRoutesList = useRoutesList(pinia);
storesRoutesList.setRoutesList(dynamicRoutes[0].children as any);
setCacheTagsViewRoutes();
}
/**
*
* @description tagsView(isHide)
*/
export function setCacheTagsViewRoutes() {
// 获取有权限的路由,否则 tagsView、菜单搜索中无权限的路由也将显示
const storesTagsView = useTagsViewRoutes(pinia);
// 添加到 pinia setTagsViewRoutes 中
storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes))[0].children);
}
/**
*
* @description dynamicRoutes/@/router/route children
* @returns
*/
export function setFilterRouteEnd() {
// console.log(dynamicRoutes);
let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower];
return filterRouteEnd;
}
/**
*
* @method router.addRoute
* @description dynamicRoutes/@/router/route children
* @link https://next.router.vuejs.org/zh/api/#addroute
*/
export async function setAddRoute() {
await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
router.addRoute(route);
});
}
/**
@ -52,8 +106,9 @@ export async function initBackEndControlRoutes() {
*/
export function getBackEndControlRoutes() {
// 模拟 admin 与 test
const stores = useUserInfo();
const auth = stores.userInfos.roles[0];
const stores = useUserInfo(pinia);
const { userInfos } = storeToRefs(stores);
const auth = userInfos.value.roles[0];
// 管理员 admin
if (auth === 'admin') return menuApi.getMenuAdmin();
// 其它用户 test

View File

@ -1,8 +1,15 @@
import { RouteRecordRaw } from 'vue-router';
import { storeToRefs } from 'pinia';
import { formatTwoStageRoutes, formatFlatteningRoutes, router } from '/@/router/index';
import { dynamicRoutes, notFoundAndNoPower } from '/@/router/route';
import pinia from '/@/stores/index';
import { useUserInfo } from '/@/stores/userInfo';
import { Session } from '/@/utils/storage';
import { useUserInfo } from '/@/stores/userInfo';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
import { useRoutesList } from '/@/stores/routesList';
import { NextLoading } from '/@/utils/loading';
import { setAddRoute, setFilterMenuAndCacheTagsViewRoutes } from '/@/router/index';
// 前端控制路由
/**
*
@ -23,3 +30,118 @@ export async function initFrontEndControlRoutes() {
// 设置递归过滤有权限的路由到 vuex routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
await setFilterMenuAndCacheTagsViewRoutes();
}
/**
*
* @method router.addRoute
* @description dynamicRoutes/@/router/route children
* @link https://next.router.vuejs.org/zh/api/#addroute
*/
export async function setAddRoute() {
await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
router.addRoute(route);
});
}
/**
* /
* @method router.removeRoute
* @description dynamicRoutes/@/router/route children
* @link https://next.router.vuejs.org/zh/api/#push
*/
export async function frontEndsResetRoute() {
await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
const routeName: any = route.name;
router.hasRoute(routeName) && router.removeRoute(routeName);
});
}
/**
*
* @description dynamicRoutes/@/router/route children
* @returns
*/
export function setFilterRouteEnd() {
let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
filterRouteEnd[0].children = [...setFilterRoute(filterRouteEnd[0].children), ...notFoundAndNoPower];
return filterRouteEnd;
}
/**
*
* @description router.addRoute
* @link https://next.router.vuejs.org/zh/api/#addroute
* @param chil dynamicRoutes/@/router/route children
* @returns
*/
export function setFilterRoute(chil: any) {
const stores = useUserInfo(pinia);
const { userInfos } = storeToRefs(stores);
let filterRoute: any = [];
chil.forEach((route: any) => {
if (route.meta.roles) {
route.meta.roles.forEach((metaRoles: any) => {
userInfos.value.roles.forEach((roles: any) => {
if (metaRoles === roles) filterRoute.push({ ...route });
});
});
}
});
return filterRoute;
}
/**
*
* @description tagsView(isHide)
*/
export function setCacheTagsViewRoutes() {
// 获取有权限的路由,否则 tagsView、菜单搜索中无权限的路由也将显示
const stores = useUserInfo(pinia);
const storesTagsView = useTagsViewRoutes(pinia);
const { userInfos } = storeToRefs(stores);
let rolesRoutes = setFilterHasRolesMenu(dynamicRoutes, userInfos.value.roles);
// 添加到 pinia setTagsViewRoutes 中
storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(rolesRoutes))[0].children);
}
/**
* vuex routesList
* @description
* @description tagsView(isHide)
*/
export function setFilterMenuAndCacheTagsViewRoutes() {
const stores = useUserInfo(pinia);
const storesRoutesList = useRoutesList(pinia);
const { userInfos } = storeToRefs(stores);
storesRoutesList.setRoutesList(setFilterHasRolesMenu(dynamicRoutes[0].children, userInfos.value.roles));
setCacheTagsViewRoutes();
}
/**
* `meta.roles`
* @param roles userInfos roles
* @param route
* @returns
*/
export function hasRoles(roles: any, route: any) {
if (route.meta && route.meta.roles) return roles.some((role: any) => route.meta.roles.includes(role));
else return true;
}
/**
*
* @param routes children
* @param roles userInfos roles
* @returns `meta.roles`
*/
export function setFilterHasRolesMenu(routes: any, roles: any) {
const menu: any = [];
routes.forEach((route: any) => {
const item = { ...route };
if (hasRoles(roles, item)) {
if (item.children) item.children = setFilterHasRolesMenu(item.children, roles);
menu.push(item);
}
});
return menu;
}

View File

@ -1,53 +1,41 @@
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { createRouter, createWebHashHistory } from 'vue-router';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import pinia from '/@/stores/index';
import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
import { useKeepALiveNames } from '/@/stores/keepAliveNames';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
import { useRoutesList } from '/@/stores/routesList';
import { useThemeConfig } from '/@/stores/themeConfig';
import { Session } from '/@/utils/storage';
import { staticRoutes, dynamicRoutes } from '/@/router/route';
import { staticRoutes } from '/@/router/route';
import { initFrontEndControlRoutes } from '/@/router/frontEnd';
import { initBackEndControlRoutes } from '/@/router/backEnd';
/**
* 1isRequestRoutes false roles setFilterRoute
* 2isRequestRoutes true roles setFilterRoute
* `backEnd.ts` `frontEnd.ts` 2
*
* 1 roles userInfo
* 2
*/
// 读取 `/src/stores/themeConfig.ts` 是否开启后端控制路由配置
const storesThemeConfig = useThemeConfig(pinia);
const { themeConfig } = storeToRefs(storesThemeConfig);
const { isRequestRoutes } = themeConfig.value;
if (isRequestRoutes) staticRoutes.splice(0, 1);
/**
* Vue 使
* @method createRouter(options: RouterOptions): Router
* @link https://next.router.vuejs.org/zh/api/#createrouter
*/
const router = createRouter({
export const router = createRouter({
history: createWebHashHistory(),
routes: staticRoutes,
});
/**
* 404401
* @link https://next.router.vuejs.org/zh/guide/essentials/history-mode.html#netlify
*/
const notFoundAndNoPower = [
{
path: '/:path(.*)*',
name: 'notFound',
component: () => import('/@/views/error/404.vue'),
meta: {
title: 'message.staticRoutes.notFound',
isHide: true,
},
},
{
path: '/401',
name: 'noPower',
component: () => import('/@/views/error/401.vue'),
meta: {
title: 'message.staticRoutes.noPower',
isHide: true,
},
},
];
/**
*
* @param arr
@ -97,127 +85,7 @@ export function formatTwoStageRoutes(arr: any) {
return newArr;
}
/**
*
* @description tagsView(isHide)
*/
export function setCacheTagsViewRoutes() {
// 获取有权限的路由,否则 tagsView、菜单搜索中无权限的路由也将显示
const stores = useUserInfo(pinia);
const storesTagsView = useTagsViewRoutes(pinia);
const { userInfos } = storeToRefs(stores);
let rolesRoutes = setFilterHasRolesMenu(dynamicRoutes, userInfos.value.roles);
// 添加到 pinia setTagsViewRoutes 中
storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(rolesRoutes))[0].children);
}
/**
* `meta.roles`
* @param roles userInfos roles
* @param route
* @returns
*/
export function hasRoles(roles: any, route: any) {
if (route.meta && route.meta.roles) return roles.some((role: any) => route.meta.roles.includes(role));
else return true;
}
/**
*
* @param routes children
* @param roles userInfos roles
* @returns `meta.roles`
*/
export function setFilterHasRolesMenu(routes: any, roles: any) {
const menu: any = [];
routes.forEach((route: any) => {
const item = { ...route };
if (hasRoles(roles, item)) {
if (item.children) item.children = setFilterHasRolesMenu(item.children, roles);
menu.push(item);
}
});
return menu;
}
/**
* vuex routesList
* @description
* @description tagsView(isHide)
*/
export function setFilterMenuAndCacheTagsViewRoutes() {
const stores = useUserInfo(pinia);
const storesRoutesList = useRoutesList(pinia);
const { userInfos } = storeToRefs(stores);
storesRoutesList.setRoutesList(setFilterHasRolesMenu(dynamicRoutes[0].children, userInfos.value.roles));
setCacheTagsViewRoutes();
}
/**
*
* @description router.addRoute
* @link https://next.router.vuejs.org/zh/api/#addroute
* @param chil dynamicRoutes/@/router/route children
* @returns
*/
export function setFilterRoute(chil: any) {
const stores = useUserInfo(pinia);
const { userInfos } = storeToRefs(stores);
let filterRoute: any = [];
chil.forEach((route: any) => {
if (route.meta.roles) {
route.meta.roles.forEach((metaRoles: any) => {
userInfos.value.roles.forEach((roles: any) => {
if (metaRoles === roles) filterRoute.push({ ...route });
});
});
}
});
return filterRoute;
}
/**
*
* @description dynamicRoutes/@/router/route children
* @returns
*/
export function setFilterRouteEnd() {
let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
filterRouteEnd[0].children = [...setFilterRoute(filterRouteEnd[0].children), ...notFoundAndNoPower];
return filterRouteEnd;
}
/**
*
* @method router.addRoute
* @description dynamicRoutes/@/router/route children
* @link https://next.router.vuejs.org/zh/api/#addroute
*/
export async function setAddRoute() {
await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
const routeName: any = route.name;
if (!router.hasRoute(routeName)) router.addRoute(route);
});
}
/**
* /
* @method router.removeRoute
* @description dynamicRoutes/@/router/route children
* @link https://next.router.vuejs.org/zh/api/#push
*/
export async function resetRoute() {
await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
const routeName: any = route.name;
router.hasRoute(routeName) && router.removeRoute(routeName);
});
}
// isRequestRoutes 为 true则开启后端控制路由路径`/src/stores/themeConfig.ts`
const storesThemeConfig = useThemeConfig(pinia);
const { themeConfig } = storeToRefs(storesThemeConfig);
const { isRequestRoutes } = themeConfig.value;
// 前端控制路由:初始化方法,防止刷新时路由丢失
if (!isRequestRoutes) initFrontEndControlRoutes();
// 路由加载前
@ -232,7 +100,6 @@ router.beforeEach(async (to, from, next) => {
if (!token) {
next(`/login?redirect=${to.path}&params=${JSON.stringify(to.query ? to.query : to.params)}`);
Session.clear();
resetRoute();
NProgress.done();
} else if (token && to.path === '/login') {
next('/home');

View File

@ -16,6 +16,7 @@ import { RouteRecordRaw } from 'vue-router';
/**
*
* `children 数组`
* @description isRequestRoutes true 使 children
* @description `/@/views/system/menu/component/addMenu.vue 下的 ruleForm`
* @returns
@ -1132,11 +1133,49 @@ export const dynamicRoutes: Array<RouteRecordRaw> = [
];
/**
*
* 404401
* @link https://next.router.vuejs.org/zh/guide/essentials/history-mode.html#netlify
*/
export const notFoundAndNoPower = [
{
path: '/:path(.*)*',
name: 'notFound',
component: () => import('/@/views/error/404.vue'),
meta: {
title: 'message.staticRoutes.notFound',
isHide: true,
},
},
{
path: '/401',
name: 'noPower',
component: () => import('/@/views/error/401.vue'),
meta: {
title: 'message.staticRoutes.noPower',
isHide: true,
},
},
];
/**
*
* `dynamicRoutes 数组`
* @description dynamicRoutes dynamicRoutes children layout
* @returns
*/
export const staticRoutes: Array<RouteRecordRaw> = [
{
path: '/',
name: '/',
component: () => import('/@/layout/index.vue'),
meta: {
title: '布局界面',
},
children: [
// 请不要往这里 `children` 中添加内容,此内容为了防止 No match found for location with path "xxx" 问题
...notFoundAndNoPower,
],
},
{
path: '/login',
name: 'login',

View File

@ -10,7 +10,7 @@ export const useRequestOldRoutes = defineStore('requestOldRoutes', {
requestOldRoutes: [],
}),
actions: {
async setCacheKeepAlive(routes: Array<string>) {
async setRequestOldRoutes(routes: Array<string>) {
this.requestOldRoutes = routes;
},
},

View File

@ -33,3 +33,13 @@
}
}
}
/* 页面宽度小于1200px
------------------------------- */
@media screen and (max-width: $lg) {
.error {
.error-flex {
padding: 0 30px;
}
}
}

View File

@ -7,6 +7,7 @@
margin-right: 5px;
width: 24px;
text-align: center;
justify-content: center;
}
/* 文本不换行

View File

@ -26,7 +26,7 @@
import { toRefs, reactive, onMounted, defineComponent } from 'vue';
import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
import { resetRoute, setAddRoute, setFilterMenuAndCacheTagsViewRoutes } from '/@/router/index';
import { frontEndsResetRoute, setAddRoute, setFilterMenuAndCacheTagsViewRoutes } from '/@/router/frontEnd';
import { Session } from '/@/utils/storage';
export default defineComponent({
@ -45,7 +45,7 @@ export default defineComponent({
//
const onRadioChange = async () => {
//
resetRoute();
frontEndsResetRoute();
let defaultRoles: string[] = [];
let defaultAuthBtnList: string[] = [];
// admin meta.roles/

View File

@ -93,6 +93,7 @@ import { toRefs, reactive, onMounted, onUnmounted, getCurrentInstance, defineCom
import * as echarts from 'echarts';
import 'echarts/extension/bmap/bmap';
import { formatDate } from '/@/utils/formatTime';
import { NextLoading } from '/@/utils/loading';
import { echartsMapList, echartsMapData, echartsMapImgs } from './mock/demo1';
//
@ -934,6 +935,7 @@ export default defineComponent({
};
//
onMounted(async () => {
NextLoading.done();
initTime();
await initEchartsMap();
await initVisualizingContentLeftTop();

View File

@ -233,6 +233,7 @@ import { toRefs, reactive, onMounted, onUnmounted, getCurrentInstance, defineCom
import * as echarts from 'echarts';
import 'echarts-gl';
import { formatDate } from '/@/utils/formatTime';
import { NextLoading } from '/@/utils/loading';
import { dropdownList, skyList, dBtnList, earth3DBtnList, chartData4List } from './mock/demo2';
import worldImg from './images/world.jpg';
import bathymetryImg from './images/bathymetry.jpg';
@ -765,6 +766,7 @@ export default defineComponent({
};
//
onMounted(async () => {
NextLoading.done();
initTime();
await initRightChartData1();
await initRightChartData4();