← Back to Vue.js Course | Chapter 1: Introduction | Lesson 5 of 7

First Vue app

A Vue app is a tiny object describing your data and methods, mounted onto an element of the page.

In this page:

  1. First Vue app
Syntax
markup
<div id="app">{{ message }}</div>

<script>
  Vue.createApp({
    data() {
      return { message: 'text' };
    }
  }).mount('#app');
</script>

First Vue app

createApp receives an options object with data, methods and more, and mount attaches the app to a DOM element. Inside the element, mustache syntax and directives are processed by Vue.

Clicking the button below changes data and the text updates automatically.

Note: Mount on a container element, not on the body itself.

Example: First Vue app

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
  <p>Count: {{ count }}</p>
  <button @click="increment">Add one</button>
</div>
<script>
  Vue.createApp({
    data() {
      return { count: 0 };
    },
    methods: {
      increment() { this.count++; },
    },
  }).mount("#app");
  document.querySelector("button").click();
  document.querySelector("button").click();
  Vue.nextTick(() => console.log("count text:", document.querySelector("p").textContent));
</script>
</body>
</html>

<!-- Output:
Rendered: Count: 2Add one
console: count text: Count: 2
-->
Live Example
Related Topics
Common Mistakes
  1. Mounting on a missing selector
  2. Mutating the DOM directly
  3. Forgetting to return data from data()
Chapter Summary
  • createApp(options).mount(selector)
  • data returns reactive state
  • methods change state
  • The DOM updates automatically
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.