优化服务启动流程,修复后端配置组件名称和vue组件名称不一致无法缓存问题

This commit is contained in:
孟帅
2023-05-29 11:54:51 +08:00
parent b353728009
commit bfcbfe55c2
18 changed files with 290 additions and 272 deletions

View File

@@ -1,6 +1,7 @@
<template>
<RouterView>
<template #default="{ Component, route }">
{{ retryKeepAlive(route) }}
<transition :name="getTransitionName" mode="out-in" appear>
<keep-alive v-if="keepAliveComponents" :include="keepAliveComponents">
<component :is="Component" :key="route.fullPath" />
@@ -15,6 +16,7 @@
import { defineComponent, computed, unref } from 'vue';
import { useAsyncRouteStore } from '@/store/modules/asyncRoute';
import { useProjectSetting } from '@/hooks/setting/useProjectSetting';
import { useRouter } from 'vue-router';
export default defineComponent({
name: 'MainView',
@@ -30,6 +32,7 @@
},
},
setup() {
const router = useRouter();
const { getIsPageAnimate, getPageAnimateType } = useProjectSetting();
const asyncRouteStore = useAsyncRouteStore();
// 需要缓存的路由组件
@@ -39,9 +42,41 @@
return unref(getIsPageAnimate) ? unref(getPageAnimateType) : '';
});
function getCurrentComponentName() {
const currentMatched = router.currentRoute.value.matched;
const currentComponent = currentMatched[currentMatched.length - 1].components.default;
return currentComponent.name || currentComponent.__name;
}
function retryKeepAlive(route) {
if (!route?.meta?.keepAlive) {
return;
}
const currentName = getCurrentComponentName();
if (currentName === undefined || route.name === undefined) {
return;
}
const index = keepAliveComponents.value.findIndex((name) => name === route.name);
if (index > -1 && currentName !== route.name) {
const index2 = keepAliveComponents.value.findIndex((name) => name === currentName);
if (index2 === -1) {
console.warn(
`the routing name configured on the backend is inconsistent with the component name in the. vue file. KeepAlive has been retried based on the actual component name, but this may cause unexpected issues.\n routeName:` +
route.name +
',currentName:' +
currentName
);
asyncRouteStore.keepAliveComponents.push(currentName);
}
}
}
return {
keepAliveComponents,
getTransitionName,
retryKeepAlive,
};
},
});

View File

@@ -61,14 +61,14 @@
</div>
</template>
<script lang="ts" setup>
<script lang="ts" setup name="login_log_index">
import { h, reactive, ref } from 'vue';
import { useDialog, useMessage } from 'naive-ui';
import { BasicTable, TableAction } from '@/components/Table';
import { BasicForm, useForm } from '@/components/Form/index';
import { usePermission } from '@/hooks/web/usePermission';
import { List, Export, Delete } from '@/api/loginLog';
import { State, columns, schemas } from './model';
import { columns, schemas } from './model';
import { ExportOutlined, DeleteOutlined } from '@vicons/antd';
import { useRouter } from 'vue-router';

View File

@@ -102,16 +102,9 @@
</div>
</template>
<script lang="ts" setup>
<script lang="ts" setup name="org_dept">
import { h, onMounted, ref } from 'vue';
import {
DataTableColumns,
NButton,
NTag,
TreeSelectOption,
useDialog,
useMessage,
} from 'naive-ui';
import { DataTableColumns, NButton, NTag, useDialog, useMessage } from 'naive-ui';
import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
import { PlusOutlined } from '@vicons/antd';
import { TableAction } from '@/components/Table';
@@ -278,17 +271,12 @@
);
},
},
// {
// title: '排序',
// key: 'sort',
// width: 80,
// },
{
title: '创建时间',
key: 'createdAt',
width: 150,
render: (rows, _) => {
return rows.createdAt; //timestampToTime();
return rows.createdAt;
},
},
{
@@ -416,10 +404,7 @@
await loadDataTable({});
});
function handleUpdateValue(
value: string | number | Array<string | number> | null,
_option: TreeSelectOption | null | Array<TreeSelectOption | null>
) {
function handleUpdateValue(value) {
formParams.value.pid = value;
}
</script>

View File

@@ -96,7 +96,7 @@
</div>
</template>
<script lang="ts" setup>
<script lang="ts" setup name="org_post">
import { h, reactive, ref } from 'vue';
import { useDialog, useMessage } from 'naive-ui';
import { BasicTable, TableAction } from '@/components/Table';