2022-06-20 10:38:49 +02:00
|
|
|
<template>
|
2023-04-08 02:01:42 +02:00
|
|
|
<KeepAlive :max="defaultStore.state.numberOfPageCache">
|
|
|
|
<Suspense>
|
|
|
|
<component
|
|
|
|
:is="currentPageComponent"
|
|
|
|
:key="key"
|
|
|
|
v-bind="Object.fromEntries(currentPageProps)"
|
2023-04-29 01:06:21 +02:00
|
|
|
tabindex="-1"
|
|
|
|
v-focus
|
2023-04-29 05:26:19 +02:00
|
|
|
style="outline: none;"
|
2023-04-08 02:01:42 +02:00
|
|
|
/>
|
|
|
|
|
|
|
|
<template #fallback>
|
|
|
|
<MkLoading />
|
|
|
|
</template>
|
|
|
|
</Suspense>
|
|
|
|
</KeepAlive>
|
2022-06-20 10:38:49 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-04-08 02:01:42 +02:00
|
|
|
import {
|
|
|
|
inject,
|
|
|
|
nextTick,
|
|
|
|
onBeforeUnmount,
|
|
|
|
onMounted,
|
|
|
|
onUnmounted,
|
|
|
|
provide,
|
|
|
|
watch,
|
|
|
|
} from "vue";
|
|
|
|
import { Resolved, Router } from "@/nirax";
|
|
|
|
import { defaultStore } from "@/store";
|
2022-06-20 10:38:49 +02:00
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
router?: Router;
|
|
|
|
}>();
|
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const router = props.router ?? inject("router");
|
2022-06-20 10:38:49 +02:00
|
|
|
|
|
|
|
if (router == null) {
|
2023-04-08 02:01:42 +02:00
|
|
|
throw new Error("no router provided");
|
2022-06-20 10:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const currentDepth = inject("routerCurrentDepth", 0);
|
|
|
|
provide("routerCurrentDepth", currentDepth + 1);
|
2022-07-20 12:59:27 +02:00
|
|
|
|
|
|
|
function resolveNested(current: Resolved, d = 0): Resolved | null {
|
|
|
|
if (d === currentDepth) {
|
|
|
|
return current;
|
|
|
|
} else {
|
|
|
|
if (current.child) {
|
|
|
|
return resolveNested(current.child, d + 1);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 10:38:49 +02:00
|
|
|
|
2022-07-20 12:59:27 +02:00
|
|
|
const current = resolveNested(router.current)!;
|
|
|
|
let currentPageComponent = $shallowRef(current.route.component);
|
|
|
|
let currentPageProps = $ref(current.props);
|
2023-04-08 02:01:42 +02:00
|
|
|
let key = $ref(
|
|
|
|
current.route.path + JSON.stringify(Object.fromEntries(current.props))
|
|
|
|
);
|
2022-07-20 12:59:27 +02:00
|
|
|
|
|
|
|
function onChange({ resolved, key: newKey }) {
|
|
|
|
const current = resolveNested(resolved);
|
|
|
|
if (current == null) return;
|
|
|
|
currentPageComponent = current.route.component;
|
|
|
|
currentPageProps = current.props;
|
2023-04-08 02:01:42 +02:00
|
|
|
key =
|
|
|
|
current.route.path + JSON.stringify(Object.fromEntries(current.props));
|
2022-06-20 10:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
router.addListener("change", onChange);
|
2022-06-20 10:38:49 +02:00
|
|
|
|
2022-07-20 12:59:27 +02:00
|
|
|
onBeforeUnmount(() => {
|
2023-04-08 02:01:42 +02:00
|
|
|
router.removeListener("change", onChange);
|
2022-06-20 10:38:49 +02:00
|
|
|
});
|
|
|
|
</script>
|