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