YouTip LogoYouTip

Vue3 Api App Mount

# Vue3 app.mount() Function Detailed Explanation * * Vue3 Global API](#) In Vue3, `app.mount()` is used to mount the Vue application to a DOM element. * * * ## 1. Basic Structure of Vue3 Application In Vue3, we first need to create a Vue application instance. Usually, we can create an application instance through the `createApp()` function: ## Example import{ createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); In this example, the `createApp()` function receives a root component `App` as a parameter and returns an application instance `app`. Next, we need to mount this application instance to a DOM element. * * * ## 2. The Role of app.mount() Function The `app.mount()` function is used to mount the Vue application instance to a specified DOM element. Its syntax is as follows: ## Example app.mount(selector); Where `selector` is a string representing the selector of the target DOM element. Vue will find the first element that matches the selector and mount the application instance to that element. ### 2.1 Basic Usage Assuming we have a `div` element in our HTML file with an `id` of `app`: ## Example
We can mount the Vue application to this `div` element with the following code: ## Example app.mount('#app'); This code will find the `div` element with `id` of `app` and mount the Vue application instance to that element. After mounting, Vue will start rendering the root component `App` and insert its content into the `div` element. ### 2.2 Notes * **Uniqueness of selector**: The `app.mount()` function will only mount to the first element that matches the selector. If there are multiple matching elements on the page, Vue will only mount to the first element. * **Mounting timing**: `
← Vue3 Api App OnunmountVue3 Api Createapp β†’