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

vue3 createApp() Function

Vue3 createApp() function


Vue3 Global API Vue 3 Global API

In Vue 3,createApp() is a global function used to create a new Vue application instance.

createApp() It is the starting point of a Vue application; all components, plugins, routes, and so on must be registered and configured through this instance.

Pass createApp(), you can easily create, configure, and manage Vue applications and mount them onto DOM elements in the page.

Unlike new Vue() in Vue 2, Vue 3 introduced createApp() to create application instances, making the Vue API more modular and flexible.

The function's syntax is as follows:

function createApp(rootComponent: Component, rootProps?: object): App

The first parameter is the root component. The second parameter is optional and is the props to be passed to the root component.


createApp() Basic Usage of

To use createApp(): First, import it from the Vue library. Here is a simple example:

Example

// Import the createApp function from the Vue library

import

{

createApp

}

from

'vue'

;

// Create a root component

const

App

=

{

  template

:

`

<

h1

Hello

,

Vue3

!

h1

`

}

;

// Use createApp to create an application instance and mount it to a DOM element

const

app

=

createApp

(

App

)

;

app.

mount

(

'#app'

)

;

In this example, we first imported vue from the library createApp function. Then, we defined a simple root component Appthat simply displays a title on the page. Next, we used createApp(App) to create a Vue application instance and mounted it to the DOM element with the ID app in the page.


createApp() Return Value of

createApp() The function returns an application instance object that provides many methods and properties for configuring and managing the Vue application.

For example, you can use the .mount() method to mount the application to a DOM element, or use the .use() method to install plugins:

Example

const

app

=

createApp

(

App

)

;

// Install the Vue Router plugin

app.

use

(

router

)

;

// mount application

app.

mount

(

'#app'

)

;


Why Use createApp()?

createApp() replaces new Vue()in Vue 3, mainly for the following reasons:


Application in Real Projects

In real projects,createApp() is typically used to create the main application instance and register global components, plugins, routes, and so on.

Here is a more complex example:

Example

import

{

createApp

}

from

'vue'

;

import

App from

'./App.vue'

;

import

router from

'./router'

;

import

store from

'./store'

;

const

app

=

createApp

(

App

)

;

// Use routing

app.

use

(

router

)

;

// Use state management

app.

use

(

store

)

;

// Register a global component

app.

component

(

'MyComponent'

,

MyComponent

)

;

// mount application

app.

mount

(

'#app'

)

;

In this example, we created a Vue application instance, then installed the routing plugin and state management plugin in sequence, and registered a global component. Finally, we mounted the application to the #app element in the page.


Vue3 Global API Vue 3 Global API