refactor: keep alive

This commit is contained in:
张传龙 2022-09-09 09:53:49 +08:00
parent a5a3472486
commit 40483e09e6
8 changed files with 65 additions and 6 deletions

View File

@ -1,11 +1,19 @@
<template> <template>
<router-view v-slot="{ Component }"> <router-view v-slot="{ Component, route }">
<component :is="Component" v-if="appStore.reloadFlag" /> <KeepAlive :include="keepAliveRouteNames">
<component :is="Component" v-if="appStore.reloadFlag" :key="route.meta?.key || route.fullPath" />
</KeepAlive>
</router-view> </router-view>
</template> </template>
<script setup> <script setup>
import { useAppStore } from '@/store/modules/app' import { useAppStore } from '@/store/modules/app'
import { useRouter } from 'vue-router'
const appStore = useAppStore() const appStore = useAppStore()
const router = useRouter()
const allRoutes = router.getRoutes()
const keepAliveRouteNames = computed(() => {
return allRoutes.filter((route) => route.meta?.keepAlive).map((route) => route.name)
})
</script> </script>

View File

@ -6,7 +6,7 @@
:collapsed-icon-size="22" :collapsed-icon-size="22"
:collapsed-width="64" :collapsed-width="64"
:options="menuOptions" :options="menuOptions"
:value="(currentRoute.meta && currentRoute.meta.activeMenu) || currentRoute.name" :value="curRoute.meta?.activeMenu || curRoute.name"
@update:value="handleMenuSelect" @update:value="handleMenuSelect"
/> />
</template> </template>
@ -19,9 +19,9 @@ import { useAppStore } from '@/store/modules/app'
import { renderCustomIcon, renderIcon } from '@/utils/icon' import { renderCustomIcon, renderIcon } from '@/utils/icon'
const router = useRouter() const router = useRouter()
const curRoute = useRoute()
const permissionStore = usePermissionStore() const permissionStore = usePermissionStore()
const appStore = useAppStore() const appStore = useAppStore()
const { currentRoute } = router
const menuOptions = computed(() => { const menuOptions = computed(() => {
return permissionStore.menus.map((item) => getMenuItem(item)).sort((a, b) => a.order - b.order) return permissionStore.menus.map((item) => getMenuItem(item)).sort((a, b) => a.order - b.order)
@ -87,7 +87,7 @@ function handleMenuSelect(key, item) {
if (isExternal(item.path)) { if (isExternal(item.path)) {
window.open(item.path) window.open(item.path)
} else { } else {
if (item.path === currentRoute.value.path) { if (item.path === curRoute.path) {
appStore.reloadPage() appStore.reloadPage()
} else { } else {
router.push(item.path) router.push(item.path)

View File

@ -72,10 +72,15 @@ const options = computed(() => [
}, },
]) ])
const route = useRoute()
const actionMap = new Map([ const actionMap = new Map([
[ [
'reload', 'reload',
() => { () => {
if (route.meta?.keepAlive) {
// keepAlive
route.meta.key = +new Date()
}
appStore.reloadPage() appStore.reloadPage()
}, },
], ],

View File

@ -0,0 +1,30 @@
<template>
<CommonPage>
<div w-350>
<n-input v-model:value="inputVal" />
<n-input-number v-model:value="number" mt-30 />
</div>
</CommonPage>
</template>
<script setup>
defineOptions({ name: 'KeepAlive' })
const inputVal = ref('')
const number = ref(0)
onMounted(() => {
$message.success('onMounted')
})
onUnmounted(() => {
$message.error('onUnmounted')
})
onActivated(() => {
$message.info('onActivated')
})
onDeactivated(() => {
$message.warning('onDeactivated')
})
</script>

View File

@ -38,5 +38,15 @@ export default {
icon: 'material-symbols:auto-awesome-outline-rounded', icon: 'material-symbols:auto-awesome-outline-rounded',
}, },
}, },
{
name: 'KeepAlive',
path: 'keep-alive',
component: () => import('./keep-alive/index.vue'),
meta: {
title: 'KeepAlive',
icon: 'material-symbols:auto-awesome-outline-rounded',
keepAlive: true,
},
},
], ],
} }

View File

@ -19,6 +19,8 @@
import MdEditor from 'md-editor-v3' import MdEditor from 'md-editor-v3'
import 'md-editor-v3/lib/style.css' import 'md-editor-v3/lib/style.css'
defineOptions({ name: 'MDEditor' })
// refs // refs
let post = ref({}) let post = ref({})
let btnLoading = ref(false) let btnLoading = ref(false)

View File

@ -22,6 +22,7 @@ export default {
icon: 'ic:baseline-table-view', icon: 'ic:baseline-table-view',
role: ['admin'], role: ['admin'],
requireAuth: true, requireAuth: true,
keepAlive: true,
}, },
}, },
{ {
@ -33,6 +34,7 @@ export default {
icon: 'ri:markdown-line', icon: 'ri:markdown-line',
role: ['admin'], role: ['admin'],
requireAuth: true, requireAuth: true,
keepAlive: true,
}, },
}, },
], ],

View File

@ -88,6 +88,8 @@ import { useCRUD } from '@/composables'
import api from './api' import api from './api'
import { isNullOrUndef } from '@/utils/is' import { isNullOrUndef } from '@/utils/is'
defineOptions({ name: 'CrudTable' })
const $table = ref(null) const $table = ref(null)
/** QueryBar筛选参数可选 */ /** QueryBar筛选参数可选 */
const queryItems = ref({}) const queryItems = ref({})