use router for header linking
load stylesheets on route change
This commit is contained in:
parent
6577c37cb8
commit
352f289b34
2 changed files with 22 additions and 7 deletions
|
@ -1,9 +1,14 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { computed } from 'vue'
|
||||||
import { type HeaderEntry } from 'content/routes.js'
|
import { type HeaderEntry } from 'content/routes.js'
|
||||||
|
import { useRouteStore } from 'src/routes'
|
||||||
|
|
||||||
defineProps<{
|
const props = defineProps<{
|
||||||
entry: HeaderEntry,
|
entry: HeaderEntry,
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
const routeStore = useRouteStore()
|
||||||
|
const name = computed(() => !!(props.entry as any).path ? routeStore._routes[(props.entry as any).path].name : null)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template lang="pug">
|
<template lang="pug">
|
||||||
|
@ -16,8 +21,8 @@ li.header-entry
|
||||||
v-for='entry in entry.children'
|
v-for='entry in entry.children'
|
||||||
:entry='entry'
|
:entry='entry'
|
||||||
)
|
)
|
||||||
a(
|
router-link(
|
||||||
v-else
|
v-else
|
||||||
:href='entry.path'
|
:to='{ name }'
|
||||||
) {{ entry.displayName }}
|
) {{ entry.displayName }}
|
||||||
</template>
|
</template>
|
||||||
|
|
18
src/main.vue
18
src/main.vue
|
@ -1,5 +1,6 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onMounted, ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
|
||||||
import { getCurrentRoute } from 'src/utilities/vuetils'
|
import { getCurrentRoute } from 'src/utilities/vuetils'
|
||||||
import { useRouteStore } from 'src/routes'
|
import { useRouteStore } from 'src/routes'
|
||||||
|
@ -8,15 +9,17 @@ import HeaderLink from 'src/components/shared/header-link.vue'
|
||||||
|
|
||||||
const ready = ref(false)
|
const ready = ref(false)
|
||||||
|
|
||||||
const currentRoute = getCurrentRoute()
|
const router = useRouter()
|
||||||
const routeStore = useRouteStore()
|
const routeStore = useRouteStore()
|
||||||
const routeConfig = routeStore._routes[currentRoute.path]
|
|
||||||
const headerConfig = routeStore._header
|
const headerConfig = routeStore._header
|
||||||
|
|
||||||
onMounted(async () => {
|
const refresh = async () => {
|
||||||
|
const currentRoute = getCurrentRoute()
|
||||||
|
const routeConfig = routeStore._routes[currentRoute.path]
|
||||||
|
|
||||||
const staleStylesheets = document.head.querySelectorAll('link[rel="stylesheet"]')
|
const staleStylesheets = document.head.querySelectorAll('link[rel="stylesheet"]')
|
||||||
staleStylesheets.forEach(stylesheet => {
|
staleStylesheets.forEach(stylesheet => {
|
||||||
document.removeChild(stylesheet)
|
document.head.removeChild(stylesheet)
|
||||||
})
|
})
|
||||||
|
|
||||||
routeConfig.stylesheetUrls?.forEach(stylesheet => {
|
routeConfig.stylesheetUrls?.forEach(stylesheet => {
|
||||||
|
@ -25,6 +28,13 @@ onMounted(async () => {
|
||||||
newElement.setAttribute('href', stylesheet)
|
newElement.setAttribute('href', stylesheet)
|
||||||
document.head.appendChild(newElement)
|
document.head.appendChild(newElement)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
await refresh()
|
||||||
|
router.afterEach(async (to, from) => {
|
||||||
|
await refresh()
|
||||||
|
})
|
||||||
ready.value = true
|
ready.value = true
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Reference in a new issue