Vue.js Components are fundamental building blocks for developing reusable and modular UI elements in Vue applications. They encapsulate HTML, CSS, and JavaScript logic into reusable components, promoting maintainability, reusability, and separation of concerns. This chapter will provide a comprehensive guide to Vue.js components, covering everything from basic concepts to advanced techniques, with detailed examples and explanations.
Vue.js Components are reusable Vue instances with a name that can be registered globally or locally within another Vue instance. They encapsulate HTML, CSS, and JavaScript logic to create custom, reusable elements.
Components can be registered globally using Vue.component
or locally within another Vue instance.
Vue.component('my-component', { ... })
globally registers a component named my-component
.<my-component></my-component>
is used in the template of the root Vue instance (#app
).<div>A global Vue component!</div>
.
const MyLocalComponent = { ... }
defines a local component.components: { 'my-local-component': MyLocalComponent }
registers MyLocalComponent
locally within the root Vue instance (#app
).<div>A local Vue component!</div>
.Components can include separate sections for template, script (JavaScript), and style (CSS).
{{ title }}
Counter: {{ counter }}
<template>
contains the HTML structure of the component.<script>
defines the component’s logic, including data
, methods
, and other options.<style scoped>
encapsulates component-specific styles.Props allow passing data from parent components to child components.
props: ['message']
declares a prop named message
.message="Hello from parent"
passes data from the parent (#app
) to the child component.<div>Hello from parent</div>
.Props can be validated to ensure expected types and values.
props: { age: { type: Number, required: true, ... } }
validates the age
prop as a Number
and requires its presence.<child-component :age="25"></child-component>
passes the age 25
to the child component.<div>25</div>
.Child components can emit custom events to communicate with parent components.
Parent received: {{ eventData }}
this.$emit('custom-event', 'Data from child')
emits a custom-event
with data from the child component.@custom-event="handleCustomEvent"
listens to the custom-event
in the parent (#app
).Parent components can handle custom events emitted by child components using event listeners.
Parent received: {{ eventData }}
this.$emit('custom-event', 'Data from child')
emits a custom-event
with data from the child component.@custom-event="handleCustomEvent"
listens to the custom-event
in the parent (#app
).Vue.js components go through various stages during their lifecycle, each stage represented by a lifecycle hook.
beforeCreate
, created
, beforeMount
, mounted
, beforeUpdate
, updated
, beforeDestroy
, destroyed
) corresponds to a different stage in the component’s lifecycle.Parent components pass data to child components using props.
:message="parentMessage"
passes the parentMessage
data from the parent to the child component.<div>{{ message }}</div>
displays the message
prop in the child<div>Message from parent</div>
.Child components emit events to communicate with parent components.
Parent received: {{ eventData }}
this.$emit('child-event', 'Data from child')
emits a child-event
with data from the child component.@child-event="handleChildEvent"
listens to the child-event
in the parent (#app
).Communication between sibling components often requires using a common parent as a mediator or using a state management pattern like Vuex.
child-a
emits a notify
event with data to its parent (parent-component
).parent-component
receives the event and updates its data (messageFromChildA
).child-b
receives messageFromChildA
as a prop and displays it.child-b
when the button in child-a
is clicked.Named slots allow components to accept additional content from parent components.
Title
Content goes here
<template v-slot:title>
defines a named slot named title
.<slot name="title"></slot>
in the card
component renders the content passed to the title
slot.<slot></slot>
renders the default slot content.Scoped slots allow child components to access data from parent components.
<template v-slot:default="slotProps">
defines a scoped slot with slotProps
.:message="slotProps.message"
passes slotProps.message
to child-component
.child-component
.Dynamic components allow rendering different components based on conditions.
:is="currentComponent"
dynamically renders component-a
or component-b
based on currentComponent
.toggleComponent
switches between component-a
and component-b
on button click.Component A
and switch to Component B
after clicking the button.Components are a core concept in Vue.js, enabling developers to build modular, reusable UI elements. By encapsulating HTML, CSS, and JavaScript logic into components, Vue.js promotes a clean, maintainable architecture and enhances code reusability. Understanding how to create, communicate between, and optimize components is essential for building scalable Vue.js applications. By mastering Vue.js components, developers can efficiently manage complexity, improve code organization, and deliver robust user interfaces.Happy coding !❤️