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

Vue3 defineComponent() Function

Vue3 defineComponent() Function


Vue3 Global API Vue 3 Global API

defineComponent() is a function used to define a component in Vue 3. It is part of the Vue 3 Composition API, designed to provide a more flexible and type-safe way to define components. Unlike Vue.component() in Vue 2,defineComponent() allows you to organize component code in a clearer and more modular way.


Why Use defineComponent()?

  1. Type Safety:defineComponent() It provides better TypeScript support, helping you catch potential errors during development.
  2. Composition API: It supports the Vue 3 Composition API, making the code easier to reuse and maintain.
  3. Clear Code Structure: Through defineComponent() Components defined have a clearer code structure, making them easier to understand and debug.

How to Use defineComponent()?

Basic Syntax

Example

import

{

defineComponent

}

from

'vue'

;

export

default

defineComponent

(

{

  name

:

'MyComponent'

,

  props

:

{

   

// Define props

 

}

,

  setup

(

)

{

   

// Composition API logic

 

}

,

  template

:

`

   

<div div`})>

;

Detailed Explanation

  1. name property: Assigns a name to the component for easier debugging and lookup.
  2. props property: Defines the properties (props) the component receives, and specifies their types and default values.
  3. setup() function: This is the core of the Composition API, used to define the component's logic. Here you can declare reactive data, computed properties, methods, and so on.
  4. template property: Defines the component's HTML template. You can also use a Single File Component (SFC) instead of template.

Example Code

Here is a simple counter component example:

Example

import

{

defineComponent

,

ref

}

from

'vue'

;

export

default

defineComponent

(

{

  name

:

'Counter'

,

  props

:

{

    initialCount

:

{

      type

:

Number

,

     

default

:

0

   

}

 

}

,

  setup

(

props

)

{

   

const

count

=

ref

(

props.

initialCount

)

;

   

const

increment

=

(

)

=>

{

      count.

value

++;

   

}

;

   

return

{

      count

,

      increment

   

}

;

 

}

,

  template

:

`

   

<

div

<

p

Count

:

{

{

count

}

}

p

     

<

button

@

click

=

"increment"

Increment

button

div

`

}

)

;

Code Analysis

  1. ref: Used to create reactive data. In this example,count is a reactive variable.
  2. increment Method: Each time it is called,count its value is incremented by 1.
  3. template: Defines the component's UI, including displaying the current count and a button to increment it.

Comparison with Vue 2

In Vue 2, components are typically created directly by defining objects via Vue.component() or export default . This approach can make code hard to maintain in large projects. Whereas defineComponent() , through the Composition API and TypeScript support, provides a more modern and flexible way to define components.


Vue3 Global API Vue 3 Global API