feat: keepAlive优化
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
import { createRouter, createWebHashHistory } from 'vue-router';
|
import {createRouter, createWebHashHistory} from 'vue-router';
|
||||||
import NProgress from 'nprogress';
|
import NProgress from 'nprogress';
|
||||||
import 'nprogress/nprogress.css';
|
import 'nprogress/nprogress.css';
|
||||||
import pinia from '/@/stores/index';
|
import pinia from '/@/stores/index';
|
||||||
import { storeToRefs } from 'pinia';
|
import {storeToRefs} from 'pinia';
|
||||||
import { useKeepALiveNames } from '/@/stores/keepAliveNames';
|
import {useKeepALiveNames} from '/@/stores/keepAliveNames';
|
||||||
import { useRoutesList } from '/@/stores/routesList';
|
import {useRoutesList} from '/@/stores/routesList';
|
||||||
import { useThemeConfig } from '/@/stores/themeConfig';
|
import {useThemeConfig} from '/@/stores/themeConfig';
|
||||||
import { Session } from '/@/utils/storage';
|
import {Session} from '/@/utils/storage';
|
||||||
import { staticRoutes } from '/@/router/route';
|
import {staticRoutes} from '/@/router/route';
|
||||||
import { initFrontEndControlRoutes } from '/@/router/frontEnd';
|
import {initFrontEndControlRoutes} from '/@/router/frontEnd';
|
||||||
import { initBackEndControlRoutes } from '/@/router/backEnd';
|
import {initBackEndControlRoutes} from '/@/router/backEnd';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1、前端控制路由时:isRequestRoutes 为 false,需要写 roles,需要走 setFilterRoute 方法。
|
* 1、前端控制路由时:isRequestRoutes 为 false,需要写 roles,需要走 setFilterRoute 方法。
|
||||||
@@ -22,8 +22,8 @@ import { initBackEndControlRoutes } from '/@/router/backEnd';
|
|||||||
|
|
||||||
// 读取 `/src/stores/themeConfig.ts` 是否开启后端控制路由配置
|
// 读取 `/src/stores/themeConfig.ts` 是否开启后端控制路由配置
|
||||||
const storesThemeConfig = useThemeConfig(pinia);
|
const storesThemeConfig = useThemeConfig(pinia);
|
||||||
const { themeConfig } = storeToRefs(storesThemeConfig);
|
const {themeConfig} = storeToRefs(storesThemeConfig);
|
||||||
const { isRequestRoutes } = themeConfig.value;
|
const {isRequestRoutes} = themeConfig.value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个可以被 Vue 应用程序使用的路由实例
|
* 创建一个可以被 Vue 应用程序使用的路由实例
|
||||||
@@ -63,7 +63,14 @@ export function formatTwoStageRoutes(arr: any) {
|
|||||||
const cacheList: Array<string> = [];
|
const cacheList: Array<string> = [];
|
||||||
arr.forEach((v: any) => {
|
arr.forEach((v: any) => {
|
||||||
if (v.path === '/') {
|
if (v.path === '/') {
|
||||||
newArr.push({ component: v.component, name: v.name, path: v.path, redirect: v.redirect, meta: v.meta, children: [] });
|
newArr.push({
|
||||||
|
component: v.component,
|
||||||
|
name: v.name,
|
||||||
|
path: v.path,
|
||||||
|
redirect: v.redirect,
|
||||||
|
meta: v.meta,
|
||||||
|
children: []
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// 判断是否是动态路由(xx/:id/:name),用于 tagsView 等中使用
|
// 判断是否是动态路由(xx/:id/:name),用于 tagsView 等中使用
|
||||||
// 修复:https://gitee.com/lyt-top/vue-next-admin/issues/I3YX6G
|
// 修复:https://gitee.com/lyt-top/vue-next-admin/issues/I3YX6G
|
||||||
@@ -71,10 +78,10 @@ export function formatTwoStageRoutes(arr: any) {
|
|||||||
v.meta['isDynamic'] = true;
|
v.meta['isDynamic'] = true;
|
||||||
v.meta['isDynamicPath'] = v.path;
|
v.meta['isDynamicPath'] = v.path;
|
||||||
}
|
}
|
||||||
newArr[0].children.push({ ...v });
|
newArr[0].children.push({...v});
|
||||||
// 存 name 值,keep-alive 中 include 使用,实现路由的缓存
|
// 存 name 值,keep-alive 中 include 使用,实现路由的缓存
|
||||||
// 路径:/@/layout/routerView/parent.vue
|
// 路径:/@/layout/routerView/parent.vue
|
||||||
if (newArr[0].meta.isKeepAlive && v.meta.isKeepAlive) {
|
if (newArr[0].meta.isKeepAlive && v.meta.isKeepAlive && v.component_name != "") {
|
||||||
cacheList.push(v.name);
|
cacheList.push(v.name);
|
||||||
const stores = useKeepALiveNames(pinia);
|
const stores = useKeepALiveNames(pinia);
|
||||||
stores.setCacheKeepAlive(cacheList);
|
stores.setCacheKeepAlive(cacheList);
|
||||||
@@ -86,7 +93,7 @@ export function formatTwoStageRoutes(arr: any) {
|
|||||||
|
|
||||||
// 路由加载前
|
// 路由加载前
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
NProgress.configure({ showSpinner: false });
|
NProgress.configure({showSpinner: false});
|
||||||
if (to.meta.title) NProgress.start();
|
if (to.meta.title) NProgress.start();
|
||||||
const token = Session.get('token');
|
const token = Session.get('token');
|
||||||
if (to.path === '/login' && !token) {
|
if (to.path === '/login' && !token) {
|
||||||
@@ -102,18 +109,18 @@ router.beforeEach(async (to, from, next) => {
|
|||||||
NProgress.done();
|
NProgress.done();
|
||||||
} else {
|
} else {
|
||||||
const storesRoutesList = useRoutesList(pinia);
|
const storesRoutesList = useRoutesList(pinia);
|
||||||
const { routesList } = storeToRefs(storesRoutesList);
|
const {routesList} = storeToRefs(storesRoutesList);
|
||||||
if (routesList.value.length === 0) {
|
if (routesList.value.length === 0) {
|
||||||
if (isRequestRoutes) {
|
if (isRequestRoutes) {
|
||||||
// 后端控制路由:路由数据初始化,防止刷新时丢失
|
// 后端控制路由:路由数据初始化,防止刷新时丢失
|
||||||
await initBackEndControlRoutes();
|
await initBackEndControlRoutes();
|
||||||
// 动态添加路由:防止非首页刷新时跳转回首页的问题
|
// 动态添加路由:防止非首页刷新时跳转回首页的问题
|
||||||
// 确保 addRoute() 时动态添加的路由已经被完全加载上去
|
// 确保 addRoute() 时动态添加的路由已经被完全加载上去
|
||||||
next({ ...to, replace: true });
|
next({...to, replace: true});
|
||||||
} else {
|
} else {
|
||||||
// https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
|
// https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
|
||||||
await initFrontEndControlRoutes();
|
await initFrontEndControlRoutes();
|
||||||
next({ ...to, replace: true });
|
next({...to, replace: true});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export const handleMenu = (menuData: Array<any>) => {
|
|||||||
title: item.title,
|
title: item.title,
|
||||||
isLink: item.is_link,
|
isLink: item.is_link,
|
||||||
isHide: !item.visible,
|
isHide: !item.visible,
|
||||||
isKeepAlive: true,
|
isKeepAlive: item.cache,
|
||||||
isAffix: false,
|
isAffix: false,
|
||||||
isIframe: false,
|
isIframe: false,
|
||||||
roles: ['admin'],
|
roles: ['admin'],
|
||||||
|
|||||||
@@ -71,6 +71,12 @@
|
|||||||
<el-radio :label="false">禁用</el-radio>
|
<el-radio :label="false">禁用</el-radio>
|
||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
<el-form-item label="缓存">
|
||||||
|
<el-radio-group v-model="form.cache">
|
||||||
|
<el-radio :label="true">启用</el-radio>
|
||||||
|
<el-radio :label="false">禁用</el-radio>
|
||||||
|
</el-radio-group>
|
||||||
|
</el-form-item>
|
||||||
<el-form-item label="图标" prop="icon">
|
<el-form-item label="图标" prop="icon">
|
||||||
<IconSelector clearable v-model="form.icon"/>
|
<IconSelector clearable v-model="form.icon"/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
@@ -227,11 +233,12 @@ let form: Form<any> = reactive({
|
|||||||
component: '',
|
component: '',
|
||||||
web_path: '',
|
web_path: '',
|
||||||
sort: '',
|
sort: '',
|
||||||
status: '',
|
status: true,
|
||||||
is_catalog: false,
|
is_catalog: false,
|
||||||
permission: '',
|
permission: '',
|
||||||
icon: '',
|
icon: '',
|
||||||
visible: ''
|
visible: true,
|
||||||
|
cache: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
let menuPermissonList = ref([]);
|
let menuPermissonList = ref([]);
|
||||||
|
|||||||
Reference in New Issue
Block a user