0

The documentation in regards to this is quite short. I want to add a Vue component to each page in the app, without having to manually declare it in each template.

I managed to add a enhanceApp.js file and add this to it:

import MyComponent from './components/my-component'

export default ({
  Vue,
  options,
  router,
  siteData
}) => {
  Vue.component('MyComponent', MyComponent)
}

The app runs but I don't see the component anywhere. Any tips or other ways I can achieve this? Thanks!

2 Answers 2

1

You do not need to put it in enhanceApp.js, just having the component in /.vuepress/components is sufficient.

Although, if you want to keep them outside that folder, that might be the way to get Vuepress to know about them.

Use it in an md file as you would do in a Vue template,

<MyComponent></MyComponent>
Sign up to request clarification or add additional context in comments.

4 Comments

That is exactly my issue. I don't want to add it manually to each md file, but add it automatically to each and every new page.
Oh, right I didn't pick up on that. The only way I can think of is to add it to Layout.vue, which you get access to by ejecting the Vuepress theme - but then you must manually upgrade when a new version of Vupress comes along (or skip upgrades).
You must have a lot of pages!
Yes, I guess I would actually need to eject the theme, which would really be a pain in the butt in the long run. Yeah, I have several pages :D Thanks anyways
1

This is an old question and might only apply to V1 but this is simple to accomplish.

  1. Add your Vue component to .vuepress/components as usual
  2. In .vuepress/config.js add the following:
    module.exports = {
    
      // rest of config...
    
      globalUIComponents: [
        'YourComponent'
      ]
    }
    
    Don't even need to import your component.

See docs for more info.

1 Comment

This is better. Much easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.