Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue Component Instance

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:

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:

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

:

'

Hello, Vue!
'

}

)

;

const

vm

=

app.

mount

(

'#app'

)

;

console.

log

(

vm.$el

)

;

//

Hello, Vue!

$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

:

'

Hello, Vue!
'

}

)

;

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