question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recommended way to use Custom Vue components in .md

See original GitHub issue

I am looking into porting the using vue in Markdown from vuepress: https://vuepress.vuejs.org/guide/using-vue.html

If I understand correctly, vitepress doesn’t want to auto register components by convention as Vuepress does. Is this the case? I actually liked this feature, but I understand that vitepress wants to keep the moving parts as small as possible.

What is the recommended way to register the components? I see that in vue-router-next docs they are registered globally inside enhanceApp: https://github.com/vuejs/vue-router-next/search?q=HomeSponsors. Same as with this comment: https://github.com/vuejs/vitepress/issues/92#issuecomment-724645482

Should we document this way in the docs?

Some thoughts about this. It would be great that users that want to use the default theme as is, do not need to learn straight away about enhanceApp to be able to use a vue component in their markdown.

If auto registering by convention in a folder like .vitepress/components is not an option, could we import them directly in the markdown?

# Docs

This is a .md using a custom component

<CustomComponent />

## More docs

...

<script setup>
  import CustomComponent from '../components/CustomComponent.vue'
</script>

Script & style hoisting is working in vitepress: https://vuepress.vuejs.org/guide/using-vue.html#script-style-hoisting, but I tried this example to import a Component and it is not at this point.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

13reactions
yyx990803commented, Nov 28, 2020

FWIW, adding this in .md file should already work:

<script setup>
  import CustomComponent from '../components/CustomComponent.vue'
</script>
3reactions
TheDutchCodercommented, Feb 23, 2021

I use the following pattern currently:

// @ts-ignore
const modules = import.meta.globEager('../components/**/*.vue')
const components = []

for (const path in modules) {
  components.push(modules[path].default)
}

export default {
  ...DefaultTheme,
  enhanceApp({ app }) {
    components.forEach(component => {
      app.component(component.name, component)
    })
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Vue in Markdown - VitePress
If your components are going to be used in only a few places, the recommended way to use them is to importing the...
Read more >
A definitive guide to Vue 3 components - LogRocket Blog
In this article, we'll cover the basics of Vue 3 components and how to use them. Jump ahead: What are components in Vue...
Read more >
How to create, publish and use your own VueJS Component ...
Run npm publish --access public to publish the library for public access. And that's it. You're done with publishing your Vue Component library ......
Read more >
How to use Vue.js Components - Gary Woodfine
Vue Components are a handy, powerful and important utility that enables developers to create custom elements which can be reused in HTML.
Read more >
Creating our first Vue component - Learn web development
Open up App.vue again. · At the top of your <script> tag, add the following to import your ToDoItem component: import ToDoItem from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found