# The Vue Instance

# Creating a Vue Instance

Every Vue application starts by creating a new Vue instance with the createApp function:

  1. Vue.createApp(/* options */)

After the Vue instance is created, we can mount it, passing a container to mount method. For example, if we want to mount a Vue application on <div id="app"></div>, we should pass #app:

  1. Vue.createApp(/* options */).mount('#app')

Although not strictly associated with the MVVM patternThe Vue Instance - 图1, Vue’s design was partly inspired by it. As a convention, we often use the variable vm (short for ViewModel) to refer to our Vue instance.

When you create a Vue instance, you pass in an options object. The majority of this guide describes how you can use these options to create your desired behavior. For reference, you can also browse the full list of options in the API reference.

A Vue application consists of a root Vue instance created with createApp, optionally organized into a tree of nested, reusable components. For example, a todo app’s component tree might look like this:

  1. Root Instance
  2. └─ TodoList
  3. ├─ TodoItem
  4. ├─ DeleteTodoButton
  5. └─ EditTodoButton
  6. └─ TodoListFooter
  7. ├─ ClearTodosButton
  8. └─ TodoListStatistics

We’ll talk about the component system in detail later. For now, just know that all Vue components are also Vue instances, and so accept the same options object (except for a few root-specific options).

# Data and Methods

When a Vue instance is created, it adds all the properties found in its data to Vue’s reactivity system. When the values of those properties change, the view will “react”, updating to match the new values.

  1. // Our data object
  2. const data = { a: 1 }
  3. // The object is added to a Vue instance
  4. const vm = Vue.createApp({
  5. data() {
  6. return data
  7. }
  8. }).mount('#app')
  9. // Getting the property on the instance
  10. // returns the one from the original data
  11. vm.a === data.a // => true
  12. // Setting the property on the instance
  13. // also affects the original data
  14. vm.a = 2
  15. data.a // => 2

When this data changes, the view will re-render. It should be noted that properties in data are only reactive if they existed when the instance was created. That means if you add a new property, like:

  1. vm.b = 'hi'

Then changes to b will not trigger any view updates. If you know you’ll need a property later, but it starts out empty or non-existent, you’ll need to set some initial value. For example:

  1. data() {
  2. return {
  3. newTodoText: '',
  4. visitCount: 0,
  5. hideCompletedTodos: false,
  6. todos: [],
  7. error: null
  8. }
  9. }

The only exception to this being the use of Object.freeze(), which prevents existing properties from being changed, which also means the reactivity system can’t track changes.

  1. const obj = {
  2. foo: 'bar'
  3. }
  4. Object.freeze(obj)
  5. const vm = Vue.createApp({
  6. data() {
  7. return obj
  8. }
  9. }).mount('#app')
  1. <div id="app">
  2. <p>{{ foo }}</p>
  3. <!-- this will no longer update `foo`! -->
  4. <button v-on:click="foo = 'baz'">Change it</button>
  5. </div>

In addition to data properties, Vue instances expose a number of useful instance properties and methods. These are prefixed with $ to differentiate them from user-defined properties. For example:

  1. const vm = Vue.createApp({
  2. data() {
  3. return {
  4. a: 1
  5. }
  6. }
  7. }).mount('#example')
  8. vm.$data.a // => 1

In the future, you can consult the API reference for a full list of instance properties and methods.

# Instance Lifecycle Hooks

Each Vue instance goes through a series of initialization steps when it’s created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes. Along the way, it also runs functions called lifecycle hooks, giving users the opportunity to add their own code at specific stages.

For example, the created hook can be used to run code after an instance is created:

  1. Vue.createApp({
  2. data() {
  3. return {
  4. a: 1
  5. }
  6. },
  7. created() {
  8. // `this` points to the vm instance
  9. console.log('a is: ' + this.a) // => "a is: 1"
  10. }
  11. })

There are also other hooks which will be called at different stages of the instance’s lifecycle, such as mounted, updated, and unmounted. All lifecycle hooks are called with their this context pointing to the Vue instance invoking it.

TIP

Don’t use arrow functionsThe Vue Instance - 图2 on an options property or callback, such as created: () => console.log(this.a) or vm.$watch('a', newValue => this.myMethod()). Since an arrow function doesn’t have a this, this will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function.

# Lifecycle Diagram

Below is a diagram for the instance lifecycle. You don’t need to fully understand everything going on right now, but as you learn and build more, it will be a useful reference.

Vue instance lifecycle