Vue.js Complete API Cheat Sheet (Vue.js Tips and Tricks) View: Vue.js Tutorials (GitBook) View: Build, Enhance, Learn. With such a wide variety of available. Vue.js Complete API Cheat Sheet (Vue.js Tips and Tricks) View: Vue.js Tutorials (GitBook) View: Build, Enhance, Learn. With such a wide variety of available resources, there’s never been a better time to dive into JavaScript. Whether you want to master a. Vue.mixin Vue.compile Vue.version Options Lifecycle Hooks beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed. Maintained by Vue.js Tips Hosted by Marozed. The.lazy modifier: don't react on every little change, just on enter/blur. Vue.js 2 Cheatsheet.
- Directives
- Options passed to a Vue object
- Instance properties
Directives
Directives are attributes identified by the v- prefix.
| Directive | Description |
|---|---|
v-text | uses the property as the text value of the element |
v-html | uses the property as the text value of the element, interpreting HTML |
v-if | show an element only if the conditional is true |
v-else | shows an alternative element if the preceding v-if is false |
v-else-if | adds an else if block for a v-if construct |
v-show | similar to v-if, but adds the element to the DOM even if falsy. Just sets it to display: none. |
v-for | iterates over an array or iterable object |
v-on | listen to DOM events |
v-bind | reactively update an HTML attribute |
v-model | sets up a two-way binding for form inputs. used in form elements, updates the model when the user changes the form field value |
v-once | applies the property just once, and never refreshes it even if the data passed changes |
v-bind and v-on have a shorthand format:
Example of v-if / v-else / v-else-if:
Conditionals
You can embed a conditional in an expression using the ternary operator:
Working with form elements
To make the model update when the change event occurs, and not any time the user presses a key, you can use v-model.lazy instead of just v.model.
Working with input fields, v-model.trim is useful because it automatically removes whitespace.
And if you accept a number instead than a string, make sure you use v-model.number.
Modifying events
I use click as an example, but applies to all possible events
v-on:click.nativetrigger a native DOM event instead of a Vue eventv-on:click.stopstop the click event propagationv-on:click.passivemakes use of the passive option of addEventListenerv-on:click.captureuse event capturing instead of event bubblingv-on:click.selfmake sure the click event was not bubbled from a child event, but directly happened on that elementv-on:click.oncethe event will only be triggered exactly oncev-on:submit.prevent: callevent.preventDefault()on the triggered submit event, used to avoid a form submit to reload the page
For more on propagation, bubbling/capturing see my JavaScript events guide.
Mouse event modifiers
v-on:click .lefttriggers only on left mouse button clickv-on:click .righttriggers only on right mouse button clickv-on:click .middletriggers only on middle mouse button click
Submit an event only if a particular key is pressed
v-on:keyup.enterv-on:keyup.tabv-on:keyup.deletev-on:keyup.escv-on:keyup.upv-on:keyup.downv-on:keyup.leftv-on:keyup.right
Keyboard event modifiers
Only trigger the event if a particular keyboard key is also pressed:
.ctrl.alt.shift.meta(cmd on Mac, windows key on Win)
v-bind
v-bind .propbind a prop instead of an attributev-bind .cameluse camelCase for the attribute namev-bind .synca syntactic sugar that expands into av-onhandler for updating the bound value. See this.
Lifecycle Hooks
beforeCreatecalled before the app is createdcreatedcalled after the app is createdbeforeMountcalled before the app is mounted on the DOMmountedcalled after the app is mounted on the DOMbeforeDestroycalled before the app is destroyeddestroyedcalled after the app is destroyedbeforeUpdatecalled before a property is updatedupdatedcalled after a property is updatedactivatedcalled when a kept-alive component is activateddeactivatedcalled when a kept-alive component is deactivated
Built-in components
Vue provides 5 built-in components:
<component><transition><transition-group><keep-alive><slot>
Global Configuration of the Vue object
The Vue.config object has these properties, which you can modify when you create the instance:
| Property | Description |
|---|---|
silent | defaults to false, if true suppress logs and warnings |
optionMergeStrategies | allows to define a custom merging strategy for options |
devtools | defaults to true in development, and false in production. You can override those values. |
errorHandler | allows to set an error handler function. Useful to hook Sentry and other similar services |
warnHandler | allows to set a warning handler function, similar to errorHandler, but for warnings instead of errors |
ignoredElements | used to let Vue ignore custom elements defined outside of it, like Web Components. |
keyCodes | let you define custom key aliases for v-on |
performance | defaults to false. If set to true, traces the performance of Vue components in the Browser DevTools. |
productionTip | defaults to true. Set to false to disable the warning “you’re in development mode” during development in the console. |
Methods of the Vue object
| Method | Description |
|---|---|
Vue.extend | allows to subclass the Vue object, to create a custom profile |
Vue.nextTick | defers the callback to be executed after the next DOM update cycle |
Vue.set | add a property to the object |
Vue.delete | delete a property from the object |
Vue.directive | set (or get) a global directive |
Vue.filter | set (or get) a global filter |
Vue.component | set (or get) a global component |
Vue.use | install a Vue.js plugin |
Vue.mixin | set a global mixin |
Vue.compile | compile a template string into a render function |
Vue.version | returns the currently installed version of Vue |
Options passed to a Vue object
When initializing a Vue object, you pass in an object:
This object accepts a number of properties.
| Property | Description |
|---|---|
data | allows to pass a set of reactive data that will be used by the Vue app. All reactive properties must be added at initialization time, you can’t add new ones later. |
props | it’s a set of attributes that are exposed to parent components as input data. |
propsData | default data for props. Only useful during testing |
methods | a set of methods that are defined on the Vue instance |
computed | like methods, but cached internally |
watch | allows to watch properties, and call a function when they change |
Example of defining data, methods and computed properties:
DOM
elsets the DOM element where the instance mounts on. It can be a CSS Selector, or an HTMLElementtemplateis a template, represented as a string, that will replace the mounted elementrenderalternatively to define the template, you can define a template using a render functionrenderErrorset an alternative output when the function attached torenderfails
Vue instance assets
directivesthe set of directives to associate to the Vue instancefiltersthe set of filters to associate to the Vue instancecomponentsthe set of components to associate to the Vue instance
Vue composition options
parentspecifies the parent instancemixinssets an array of mixin objectsextendsextend another component

Other Vue object options
namesetting a name to the component lets you invoke it, useful in debugging or when you need to recursively add a component in its templatefunctionalif true, sets the component to be stateless (nodata) and instanceless (nothis), making it more lightweightmodelallows to customize the property used in events, useful for example when interacting with formscommentsdefaults to false. If set to true, retains the HTML comments that are put in templates
Instance properties
Given an instance of Vue, stored into a variable const vm = new Vue(/*...*/), you can inspect and interact with it.
Properties of a Vue instance
vm.$datathe data object associated to the instancevm.$propsthe props the instance has receivedvm.$elthe DOM element to which the instance is boundvm.$optionsthe object used to instantiate the Vue instancevm.$parentthe parent instancevm.$rootthe root instance (if this is the root instance, this points to itself)vm.$childrenan array of children instancesvm.$slotsan array of the associated slots contained in the templatevm.$scopedSlotsan array of the associated scoped slotsvm.$refsan object that contains a property for each element pointed by arefattribute defined in the templatevm.$isServertrue if the Vue instance is running on the server (useful in server-side rendering)vm.$attrsan object of attributes that are provided to the component but not defined as propsvm.$listenersan object ofv-onevent listeners assigned to the component
Methods Data
vm.$watchset up a watcher for property changes in the Vue data. It can also watch for value changes inside objectsvm.$setset a propertyvm.$deletedelete a property
Events
vm.$emittriggers a custom event on thevmVue instancevm.$onlisten for a custom event on thevmVue instancevm.$oncelike$on, but listens only oncevm.$offremoves an event listener from the Vue instance
Lifecycle Methods
vm.$mountmount a Vue instance on a DOM element, in case it was not mounted yetvm.$forceUpdateforce thevmVue instance to re-render. Does not force child components to rerender.vm.$nextTickaccepts a callback and schedules that for the next DOM update cyclevm.$destroydestroys the application and remove all child components, observers and listeners
Download my free Vue Handbook and check out my Vue Course!
Table of Contents
- 8. Vue CLI
- 9. Slots
- 16. Transitions
- 17. HTTP: vue-resource
- 18. Routing
1 Events
2 Styling
3 Conditionals
4 Looping
5 Components
6 Vue lifecycle
Vue ←watch→ Virtual DOM ←update→ DOM
new Vue();.beforeCreate();.created();.beforeMount();.updated();.beforeUpdate();.beforeDestroy();.destroyed();
7 Development workflow

8 Vue CLI
Different templates: simple, webpack-simple, webpack, browserify / browserify-simple
8.1 Example: webpack-simple layout
9 Slots
Passing some content (i.e. DOM trees) from outside into a component?
Parent.vue:
Child component:
9.1 Multiple slots: <slot name='>
In child.vue:
In parent.vue:
An unnamed slot is the default slot!
9.2 Optional <slot>: slots with defaults
10 Dynamic components: <component>
An old component is destroyed on change!Although:
Hooks into <keep-alive> lifecycle:
11 Forms
The .lazy modifier: don't react on every little change, just on enter/blur:
Multiple bindings to the same name in data make an array in data automatically (useful for checkboxes, <input type='checkbox'></input>).
– automatically switches the choice value.
Vue Js Quickstart
12 Using v-model with components
Component:

13 Directives: v-...
Hooks:
bind(el, binding, vnode)– Once directive is attached;inserted(el, binding, vnode)– Inserted in parent node;update(el, binding, vnode, oldVnode)– once component is updated (without children);componentUpdated(el, binding, vnode, oldVnode)– (with children);unbind(el, binding, vnode)
Arguments for a directive:
Modifiers:
Registering directives locally:
Multiple modifiers values: pass an object
14 Filters
15 Mixins
16 Transitions
<transition name='*'> elements.CSS classes:
*-enterwhen forward animation starts;*-enter-activewhen forward animation ends;*-leavewhen reverse animation starts;*-leave-activewhen reverse animation ends;
16.1appear attribute
triggers animations on load.
16.2 Manual leave-enter classes:
16.3 Dynamic transition name
16.4 Multiple elements transitions
16.5 Transition JS hooks
17 HTTP: vue-resource
17.1 Setup
or
17.2 Importing vue-resource
17.3 Usage: simple example
17.4Vue.http.interceptors hooks
Ultimate Vue.js Cheat Sheet
17.5 Setting custom resources
17.6 URL templating
18 Routing
18.1 Setting up
18.2 First route
routes.js:
main.js:
App.vue:
18.3 Routing modes (hash vs history)
'Seamless' mode: the server always returns index.html on any request
18.4 Links and navigation
18.5 Styling active links
18.6 Using navigation from Javascript

18.7 Dynamic paths
