Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 87 ยท Getting Started

Your First Vue App

Build your first Vue 3 app with createApp and mount. Learn the root component, setup and data, template strings, in-DOM templates and how the live examples work.

Every Vue application starts the same way: you create an app from a root component and mount it onto an element of the page. That is two function calls, and once you understand them, every other Vue lesson is "what can go inside the root component".

In this lesson you will build a tiny app, read it line by line, learn two ways of writing the template, and see how the live examples in this tutorial are put together so that you can edit them with confidence.

The smallest app

Press Run on this example and then read the explanation below it.

Hello, Vue

Here is what each part does:

  1. The import. Vue is an ES module. The import pulls it from a CDN (a public file server), so there is nothing to install. The "esm-browser" build contains Vue's template compiler, which is what lets us pass a template string. (Some CDN default builds leave the compiler out to save size; they only work with templates that were compiled ahead of time by a build tool. This site uses the full build.)
  2. The root component App is a plain object. Its template describes the markup, and setup prepares the data and functions the template can use.
  3. ref("Hello, Vue!") creates a reactive reference: a box holding a value that Vue watches. You read and write it with .value inside JavaScript. In the template you write just message, because templates unwrap refs for you. Reactive State with ref covers this in depth.
  4. return { message, shout } is the list of things the template may use. Anything you do not return is private to setup.
  5. createApp(App) makes an app instance. .mount("#app") renders it inside the element that matches the selector, replacing whatever was inside.
flowchart LR
    A["Root component<br/>template + setup"] --> B["createApp(App)"]
    B --> C["mount('#app')"]
    C --> D["Template compiled<br/>to a render function"]
    D --> E["DOM created<br/>inside the #app element"]

Mounting turns the root component's template into a render function once, then uses that function to build and later update the DOM.

Data with setup or data

setup is the Composition API way to create state. Vue also supports the older, still very common Options API, where you return state from a data() function and put functions in methods. Both produce the same result, and you can pick either:

JavaScript
const App = {
  data() {
    return { message: "Hello, Vue!" };
  },
  methods: {
    shout() {
      this.message = this.message.toUpperCase();
    },
  },
  template: `<h2>{{ message }}</h2><button @click="shout">Shout</button>`,
};

In the Options API you use this.message inside methods, without .value. We will use setup for most lessons and revisit the other style in The Options API.

In-DOM templates

A template string is not the only option. If you do not pass one, Vue uses the HTML that is already inside the mount element as the template. This is the original "drop Vue into an existing page" way, and it is ideal when a server already produced the markup:

Using the page HTML as the template

Notice three things. The markup is ordinary HTML in the page, so it works without JavaScript tricks. The v-cloak attribute plus the small CSS rule hides the raw {{ name }} text until Vue has finished compiling, then Vue removes the attribute. Finally, this app has no template option and no ref: the data comes from data() and the markup comes from the page.

In-DOM templates have a few HTML-parsing limits. Browsers lowercase attribute names, and they do not allow self-closing custom tags, so they are best for small pages. For everything larger, use template strings or, with a build tool, single-file components (Single-File Components).

Apps are independent

createApp returns a separate application each time. Two apps on the same page do not share state, which is handy for adding small widgets to different parts of a server-rendered page:

Two independent apps

Click one button several times: the other keeps its own number. The second argument of createApp passes props to the root component; you will study props properly in Props.

How the live examples work

Every html example in this tutorial is a complete web page that runs in a sandboxed iframe below the code. The sandbox has limits you should know about:

  • Vue comes from a CDN, so the very first run needs an internet connection. Later runs use the browser cache.
  • The sandbox cannot use localStorage, cookies or the clipboard, and it cannot post forms to a real server. Examples show results on the page instead.
  • There are no .vue files, no Vue Router and no Pinia here, because those need a build step or npm packages. When a lesson shows them, the code block is read-only and a runnable version made from plain Vue sits next to it.
  • You can edit the code in any example and run it again. Break something on purpose: that is the fastest way to learn what an error message means.

The console panel under the preview shows console.log output and errors, so use it freely.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does createApp(App).mount('#app') do?

    createApp builds an application instance from the root component, and mount renders it into the matched element.
  2. 2. Why must setup() return { message, shout } for the template to use them?

    The object returned from setup() is what the template can access. Unreturned values are private.
  3. 3. What is v-cloak for?

    Before Vue mounts, an in-DOM template shows raw {{ }} text. v-cloak plus a CSS rule hides it, and Vue removes the attribute after mounting.
  4. 4. You call createApp twice and mount the two apps on different elements. How do they share state?

    Each createApp call creates a separate application with its own component state.

Summary

  • A Vue app is created with createApp(rootComponent) and rendered with .mount(selector).
  • The root component can have a template, setup (Composition API) or data/methods (Options API).
  • setup returns what the template may use; ref creates reactive state read with .value in JavaScript.
  • Without a template, Vue compiles the HTML already inside the mount element; use v-cloak to hide it until then.
  • Each createApp call makes an independent application.
  • The live examples load Vue from a CDN and run in a sandbox without storage or network access.

Next: Ways to Use Vue