refactor: keep alive
This commit is contained in:
parent
a5a3472486
commit
40483e09e6
@ -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>
|
||||||
|
@ -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)
|
||||||
|
@ -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()
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
30
src/views/demo/base/keep-alive/index.vue
Normal file
30
src/views/demo/base/keep-alive/index.vue
Normal 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>
|
@ -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,
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
@ -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,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -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({})
|
||||||
|
Loading…
Reference in New Issue
Block a user