How to register a global component #3611
-
I have read the docs, however, I am new to Vue/Vite, therefore, I might need some help. I want to register a component composed of a Here’s the code: <script lang='ts' setup>
import {useMagicKeys} from '@vueuse/core'
import {useData, useRouter} from 'vitepress'
import {watch} from 'vue'
const keys = useMagicKeys()
const keyU = keys['u']
const u = useData()
const router = useRouter()
let oldPath = u.page.getter().relativePath.replace(/\.(md|html)$/, '')
watch(keyU, v => {
if (v) {
const newPath = '/user'
console.log(`\`u\` have been pressed, redirecting to \`${newPath}\` ...`)
if (newPath !== oldPath && !document.activeElement?.classList.contains('search-input')) {
router.go(newPath)
oldPath = newPath
}
}
})
</script> Now, this works when I inline it into a single MD file, however, when I place it into import type {Theme} from 'vitepress'
import DefaultTheme from 'vitepress/theme'
export default {
extends: DefaultTheme,
enhanceApp({app}) {
// register your custom global components
app.component('KeyboardNavigation')
}
} satisfies Theme it does not work. I have also tried to add What do I do wrong? Do I need to import that Thanks in advance! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Global components there means component that can be used anywhere without importing. You want to execute some code in each of the pages without even mentioning the component, so you need to write the Layout: // .vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme'
import Layout from './Layout.vue'
export default {
extends: DefaultTheme,
Layout
} Inside <script setup lang="ts">
import DefaultTheme from 'vitepress/theme'
// ... your rest of key handling logic ...
</script>
<template>
<DefaultTheme.Layout />
</template> |
Beta Was this translation helpful? Give feedback.
You need to use that component. Just importing does nothing. Also you should write a composable instead, not component.