Vue Component Instance
In Vue.js, a component instance is one of the most fundamental building blocks of a Vue application. Each component instance is a Vue component with its own template, data, methods, and lifecycle hooks, and can independently manage its own state and behavior. The following is a detailed introduction to Vue component instances:
Creating a Vue Component Instance
In Vue.js, creating a component instance is usually done through the createApp method and a component options object.
The component options object can contain the following properties:
data: The data object, used to store the component's reactive data.props: An array or object of props, used to receive data passed from the parent component.computed: The computed properties object, dynamically computed based on reactive dependencies.methods: The methods object, containing internal functions and operations of the component.watch: The watchers object, used to observe data changes and respond.template: A string that defines the component's template structure.render: A function used to render the component's virtual DOM.setup: A function used to set the component's initial state, data, computed properties, and watchers (in Vue 3).
Here is an example of a simple Vue component instance:
Example
import
{
createApp
}
from
'vue'
;
const
app
=
createApp
(
{
data
(
)
{
return
{
message
:
'Hello, Vue!'
}
;
}
,
methods
:
{
greet
(
)
{
alert
(
this
.
message
)
;
}
}
,
template
:
`
<div>
<p{{message}}p>
<button@click="greet" Greet button div`})>;
app.
mount
(
'#app'
)
;
Explanation:
dataThe function returns an object containingmessageproperty, used to store the component's state.methodsThe object contains agreetmethod; when the button is clicked, it pops up an alert showingmessage.templateThe string defines the component's template structure, using interpolation and event binding to achieve data display and interaction.
Lifecycle Hooks
Vue component instances have lifecycle hook functions that allow developers to execute custom logic at different stages of the component lifecycle. Common lifecycle hooks include created, mounted, updated and destroyed , etc.
Example
const
app
=
createApp
(
{
data
(
)
{
return
{
message
:
'Hello, Vue!'
}
;
}
,
created
(
)
{
console.
log
(
'Component created'
)
;
}
,
mounted
(
)
{
console.
log
(
'Component mounted'
)
;
}
,
updated
(
)
{
console.
log
(
'Component updated'
)
;
}
,
destroyed
(
)
{
console.
log
(
'Component destroyed'
)
;
}
,
template
:
`
<div>
<p{{message}}p div`})>;
Component Communication
Vue component instances communicate through props, events, provide/inject, $parent/$children, $attrs/$listeners, etc., depending on the component's relationship and needs. These mechanisms allow components to pass data between different levels and respond to user interactions.
Component Instance Properties
$data
The $data property contains all data properties of the component instance. It is reactive—when the data changes, the related view updates automatically.
Example
const
app
=
Vue.
createApp
(
{
data
(
)
{
return
{
message
:
'Hello, Vue!'
}
;
}
}
)
;
const
vm
=
app.
mount
(
'#app'
)
;
console.
log
(
vm.$data
)
;
// { message: 'Hello, Vue!' }
$props
The $props property contains all props passed from the parent component to the current component. It is read-only.
Example
Example
// ChildComponent.vue
export
default
{
props
:
[
'message'
]
,
mounted
(
)
{
console.
log
(
this
.$props.
message
)
;
// 'Message from parent'
}
}
;
$refs
$refs contains all DOM elements or child component instances within the component that have the ref attribute. You can access these elements or component instances through $refs.
Example
The $el property represents the root DOM element of the current component instance. It can only be accessed after the component is mounted.
Example
const
app
=
Vue.
createApp
(
{
template
:
'
}
)
;
const
vm
=
app.
mount
(
'#app'
)
;
console.
log
(
vm.$el
)
;
//
$attrs
$attrs contains all attributes passed from the parent component to the current component that were not declared in the child component. These attributes can be used to pass extra non-prop characteristics to the child component.
Example
$listeners
$listeners contains all listeners (event handlers) passed from the parent component to the current component, which can be directly bound to the component's root element or other elements.
Example
Component Instance Methods
$mount()
The $mount() method manually mounts the Vue component to a DOM element. In Vue 3, automatic mounting via createApp().mount() is typical; manual mounting is only needed in rare cases.
Example
const
app
=
Vue.
createApp
(
{
template
:
'
}
)
;
const
vm
=
app.
mount
(
'#app'
)
;
$destroy()
The $destroy() method destroys the current component instance, unbinds all its event listeners, and releases the resources it occupies. It is useful in specific scenarios such as dynamically creating and destroying components.
Example
vm.$destroy
(
)
;
$emit()
The $emit() method is used to trigger custom events on the current component instance and pass data to the parent component. It is usually used together with the v-on directive.
Example
Click me
$nextTick()
The $nextTick() method executes the specified callback after the next DOM update cycle ends. It is used to perform operations after the DOM has been updated.
Example
vm.$nextTick
(
(
)
=>
{
// operations performed after DOM update completes
}
)
;
$watch()
The $watch() method is used to observe data changes on the component instance and execute the specified callback when the data changes.
Example
vm.$watch
(
'message'
,
(
newValue
,
oldValue
)
=>
{
console.
log
(
`Message changed from $
{
oldValue
}
to $
{
newValue
}
`
)
;
}
)
;
These properties and methods give Vue component instances rich functionality and flexibility, meeting developers' various needs when building complex applications—from data management to event handling to dynamic component operations—all manageable and controllable through the component instance.