25

I am trying to use a globally registered component (with Vue.component) inside a single file component but I am always getting

vue.common.js:2611[Vue warn]: Unknown custom element: <my-component> - did you register the component correctly?

For example:

main.js:

...
Vue.component('my-component', {
    name: 'my-component',
    template: '<div>A custom component!</div>'
})
...

home.vue:

<template>
    <div>
        <my-component></my-component>
    </div>
</template>
<script>
    module.exports = {
        name: 'home'
    }
</script>

If I register it locally, it works OK:

<template>
    <div>
        <my-component></my-component>
    </div>
</template>
<script>
module.exports = {
    name: 'home',
    components: {
        'my-component': require('./my-component.vue')
    }
}
</script>
2
  • You need to require the component somewhere otherwise it won't be downloaded.
    – Elfayer
    Oct 30, 2016 at 18:00
  • @Elfayer Everything is within a bundle created by Vueify and the home component is required in the main.js file. Oct 30, 2016 at 18:25

2 Answers 2

51

You don't need the module.exports. You can register the component globally by having this within the mycomponent.vue file.

<template>
    <div>A custom component!</div>
</template>
<script>
    export default {}
</script>

Then add to main.js

import MyComponent from './component.vue'
Vue.component('my-component', MyComponent);

or I typically register them in a 'globals' file them import that into the main.

That should then allow you to use my-component anywhere in the app.

6
  • I use browserify / vueify and ES5. Because of that I have to use module.export and require. Jan 14, 2017 at 14:57
  • = should be removed in export default = {}
    – Alexander
    Feb 26, 2017 at 22:11
  • 2
    Thanks a lot. I spent two days reading the docs and SO to try to understanding how to globally declare a single-page component, while the syntax was simply Vue.component('my-component', MyComponent); :)
    – Overdrivr
    Aug 21, 2017 at 13:16
  • Thank you so much man ! can you please tell me whats the difference between both Vue.component('card', require('./components/card.vue')); and import Card from './components/card.vue'; Vue.component('card', Card); ? Nov 2, 2017 at 6:26
  • 1
    @PardeepJain both of these statements evaluate to the same thing. The former uses the UMD require function, which is an interim solution for including modular Javascript dependencies. The latter uses the modern ES6 import standard syntax (tho it's still in it's early days and therefore oft unsupported so any scripts which use the import statement will likely need to be transpiled to vanilla require-style JS for now.) May 14, 2018 at 14:52
3

Component.vue

<template><div>A custom component!</div></template>

<script>export default { code here... }</script>

Use this component in home.vue:

<template>
    <div>
        <my-component></my-component>
    </div>
</template>

 <script>
    import component from './component.vue'
    export default {
     components: { my-component: component }
    }
</script>
1
  • 6
    I don't want to locally register my-component, I wan't to be able to use it all over the app and because of that I want to register it globally using Vue.component Oct 30, 2016 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.