Guide
- Installation
- Getting Started
- Overview
- The Vue Instance
- Data Binding Syntax
- Computed Properties
- Class and Style Bindings
- Conditional Rendering
- List Rendering
- Methods and Event Handling
- Form Input Bindings
- Transitions
- Components
- Reactivity in Depth
- Custom Directives
- Custom Filters
- Mixins
- Plugins
- Building Large-Scale Apps
- Comparison with Other Frameworks
The Vue Instance
Constructor
Every Vue.js app is bootstrapped by creating a root Vue instance with the Vue
constructor function:
var vm = new Vue({ |
A Vue instance is essentially a ViewModel as defined in the MVVM pattern, hence the variable name vm
you will see throughout the docs.
When you instantiate a Vue instance, you need to pass in an options object which can contain options for data, template, element to mount on, methods, lifecycle callbacks and more. The full list of options can be found in the API reference.
The Vue
constructor can be extended to create reusable component constructors with pre-defined options:
var MyComponent = Vue.extend({ |
Although you can create extended instances imperatively, in most cases you will be registering a component constructor as a custom element and composing them in templates declaratively. We will talk about the component system in detail later. For now, you just need to know that all Vue.js components are essentially extended Vue instances.
Properties and Methods
Each Vue instance proxies all the properties found in its data
object:
var data = { a: 1 } |
It should be noted that only these proxied properties are reactive. If you attach a new property to the instance after it has been created, it will not trigger any view updates. We will discuss the reactivity system in details later.
In addition to data properties, Vue instances expose a number of useful instance properties and methods. These properties and methods are prefixed with $
to differentiate from proxied data properties. For example:
var data = { a: 1 } |
Consult the API reference for the full list of instance properties and methods.
Instance Lifecycle
Each Vue instance goes through a series of initialization steps when it is created - for example, it needs to set up data observation, compile the template, and create the necessary data bindings. Along the way, it will also invoke some lifecycle hooks, which give us the opportunity to execute custom logic. For example, the created
hook is called after the instance is created:
var vm = new Vue({ |
There are also other hooks which will be called at different stages of the instance’s lifecycle, for example compiled
, ready
and destroyed
. All lifecycle hooks are called with their this
context pointing to the Vue instance invoking it. Some users may have been wondering where the concept of “controllers” lives in the Vue.js world, and the answer is: there are no controllers in Vue.js. Your custom logic for a component would be split among these lifecycle hooks.
Lifecycle Diagram
Below is a diagram for the instance lifecycle. You don’t need to fully understand everything going on right now, but this diagram will be helpful in the future.