web123456

Create a Vue app

1. Application example

EachVueAll applications are throughcreateAppFunction creates a newApplication example

  1. import { createApp } from 'vue'
  2. const app = createApp({
  3. /*Root Component Options*/
  4. })

2. RootComponents 

We're passing increateAppThe object of   is actually a component, and each application requires a "root component" and other components will serve as itsSubcomponents

If you are usingSingle file component, we can import the root component directly from another file.

  1. import { createApp } from 'vue'
  2. // Import the root component from a single file component
  3. import App from './'
  4. const app = createApp(App)

While many of the examples in this guide require only one component, most real applications consist of a nested, reusable component tree. For example, a component tree for a Todos application might look like this:

  1. App (root component)
  2. ├─ TodoList
  3. │ └─ TodoItem
  4. │ ├─ TodoDeleteButton
  5. │ └─ TodoEditButton
  6. └─ TodoFooter
  7. ├─ TodoClearButton
  8. └─ TodoStatistics

We will discuss how to define and combine multiple components in subsequent chapters of the guide. Before that, we have to focus on what is happening within a component.

3. Mount the application

The application instance must be called.mount()The method will be rendered only after it is done. This method receives a "container" parameter, which can be an actual DOM element or a CSS selector string

<div id="app"></div>
('#app')

The contents of the application root component will be rendered in the container element. The container element itselfWon'tConsidered as part of the application.

.mount()Methods should always be called after the entire application configuration and resource registration are completed. Also note that unlike other resource registration methods, its return value is the root component instance rather than the application instance.

Root component intemplate 

The template for the root component is usually part of the component itself, but it can also be provided separately directly by writing the template inside the mount container:

  1. <div id="app">
  2. <button @click="count++">{{ count }}</button>
  3. </div>
  1. import { createApp } from 'vue'
  2. const app = createApp({
  3. data() {
  4. return {
  5. count: 0
  6. }
  7. }
  8. })
  9. app.mount('#app')

When the root component is not settemplateWhen the option is selected, Vue will automatically use the container'sinnerHTMLAs a template.

DOM internal templates are usually used forNo build stepsVue app. They can also be used with server-side frameworks where the root template may be dynamically generated by the server.

5. Application configuration

An application example will be exposed.configObjects allow us to configure some application-level options, such as defining an application-levelError handling, used to catch errors on all subcomponents:

  1. = (err) => {
  2. /*Handling errors*/
  3. }

Application instances also provide some methods to register available resources within the application scope, such as registering a component:

('TodoDeleteButton', TodoDeleteButton)

This makesTodoDeleteButtonAvailable anywhere in the application. We will discuss registration of components and other resources in subsequent chapters of the guide. You can alsoAPI ReferenceView the complete list of the application instance APIs in .

Make sure that all application configurations are completed before mounting the application instance!

6. Multiple application examples

Application instances are not limited to only one.createAppThe API allows you to create multiple coexisting Vue applications in the same page, and each application has its own scope for configuration and global resources.

  1. const app1 = createApp({
  2. /* ... */
  3. })
  4. app1.mount('#container-1')
  5. const app2 = createApp({
  6. /* ... */
  7. })
  8. app2.mount('#container-2')

If you are using Vue to enhance server rendering HTML and want Vue to control a special small part of a large page, you should avoid mounting a separate Vue application instance to the entire page, but instead create multiple small application instances to mount them separately to the required elements.