2

I'm really new to the Vue framework and constantly stalling at the basics. Currently I'm stuck with following scenario.

I create/build reusable components this way below:

Vue.component('SomeReusableComponent', {
    props: [ ... ],
    template: `
        <Some HTML>
    `
})

But I also saw other people creating/building components like this:

const SomeReusableComponent = {
    props: [ ... ],
    template:`
        <Some HTML>
    `
}

What is the difference between the two? What is each use case? I mean, I can also attach a Vue.component to a const and it will basically become the same, or not?

Thanks in advance

2 Answers 2

3

A vue component is nothing but a plain javascript object containing the components options like data, template, computed etc as its properties.

What is the difference between the two?

Vue.component(componentName, componentOptions) creates a global component. The component created like this can be used anyehere without registering it.

For example: Vue.component('global-comp', {template: 'This is global component')

The above created component can be used directly as:

//another-component
  <div>
    <global-comp></global-comp>
  </div>

Now the other type of component.

As said above a component is nothing but a plain object

For example:

const MyComponent ={
    template: '<div>My {{text}} component</div>',
    data(){
      return{
        text: 'awesome'
      }
    }
  }

To use the above component in another component you need to register it under components option as below

//another component
  <div>
    <my-component></my-component>
  </div>

  <script>
    //another component
    export default{
      name: 'Another',
      //this is required to register my-component if 
      //it is not global i.e not defined using Vue.component()
      components:{
        'my-component': MyComponent
      }
    }
  </script>

Note:

  • You do not want to register every component globally as it would pollute the global namespace.
  • You do not need every component used at multiple places.
  • In the future when you learn to lazy load your component, you do not want a component which the user might not need in initial bundle and increase its size

Docs- Component Registration

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this clarifying answer. May I ask you for what exactly the second example with "export" is useful?
It just how a component looks if you yuse a bundler like webpack or create a vue project using vue-cli which interna,ly uses webpack
Oh, more complex stuff... Hopefully I will get to that soon. Thanks again.
@Fusseldieb wishing you good luck and hope you get there.. :-)
2
  1. Vue.component function registers component for global usage. So if you registered Vue.component('my-awesome-component', { /* params */ }) you can use <my-awesome-component> in any another component template.
  2. When you just declare object like your second example with const you will have to register this component some how further with Vue.component function as well or in parent component components section (to use locally in parent component only).

    //in parent component
    import MyAwesomeComponent from "./my-awesome-component";
    
    export default {
    ...
        components: { MyAwesomeComponent }
    ...
    }
    

Comments

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.