8 [Vue.js](https://vuejs.org/) is an open-source Model–view–viewmodel JavaScript framework for building user interfaces and single-page applications.
21 <p>I have a {{ product }}</p>
22 <p>{{ product + 's' }}</p>
23 <p>{{ isWorking ? 'YES' : 'NO' }}</p>
24 <p>{{ product.getSalePrice() }}</p>
28 See: [Delimiters](https://vuejs.org/v2/api/#delimiters)
33 <a v-bind:href="url">...</a>
38 <a :href="url">...</a>
42 #### True or false will add or remove attribute
44 <button :disabled="isButtonDisabled">...
47 #### If isActive is truthy, the class ‘active’ will appear
49 <div :class="{ active: isActive }">...
52 #### Style color set to value of activeColor
54 <div :style="{ color: activeColor }">
57 See: [v-bind](https://vuejs.org/v2/api/#v-bind)
61 #### Element inserted/removed based on truthiness
63 <p v-if="inStock">{{ product }}</p>
66 <p v-else-if="onSale">...</p>
70 #### Toggles the display: none CSS property
72 <p v-show="showProductDetails">...</p>
75 #### Two-way data binding
77 <input v-model="firstName" >
80 | Method | Description |
82 | `v-model.lazy="..."` | Syncs input after change event |
83 | `v-model.number="..."` | Always returns a number |
84 | `v-model.trim="..."` | Strips whitespace |
86 See: [Directives](https://vuejs.org/v2/api/#Directives)
90 #### Calls addToCart method on component
92 <button v-on:click="addToCart">...
97 <button @click="addToCart">...
101 #### Arguments can be passed
103 <button @click="addToCart(product)">...
107 #### To prevent default behavior (e.g. page reload)
109 <form @submit.prevent="addProduct">...
112 #### Only trigger once
114 <img @mouseover.once="showImage">...
117 | Method | Description |
119 | `.stop` | Stop all event propagation |
120 | `.self ` | Only trigger if event.target is element itself |
122 #### Keyboard entry example
124 <input @keyup.enter="submit">
127 #### Call onCopy when control-c is pressed
129 <input @keyup.ctrl.c="onCopy">
132 See: [Events](https://vuejs.org/v2/guide/events.html)
136 #### The `:key` is always recommended
138 <li v-for="item in items"
145 #### To access the position in the array
147 <li v-for="(item, index) in items">...
150 #### To iterate through objects
152 <li v-for="(value, key) in object">...
155 #### Using `v-for` with a component
157 <cart-product v-for="item in products"
162 See: [List Rendering](https://vuejs.org/v2/guide/list.html)
168 ### Component anatomy
171 Vue.component('my-component', {
173 // Components that can be used in the template
178 // The parameters the component accepts
185 validator: function (value) {
186 // Should return true if value is valid
191 // `data` must be a function
198 // Return cached values until dependencies change
199 fullName: function () {
200 return this.firstName + ' ' + this.lastName
204 // Called when firstName changes value
205 firstName: function (value, oldValue) { ... }
208 template: '<span>{{ message }}</span>',
209 // Can also use backticks in `template` for multi-line
212 {: data-line="3, 8, 16, 21, 28, 34, 39"}
214 See: [Components Basics](https://vuejs.org/v2/guide/components.html)
218 | Method | Description |
220 | `beforeCreate` | After the instance has been initialized [#](https://vuejs.org/v2/api/#beforeCreate) |
221 | `created` | After the instance is created [#](https://vuejs.org/v2/api/#created) |
222 | `beforeMount` | Before the first render [#](https://vuejs.org/v2/api/#beforeMount) |
223 | `mounted` | After the instance has been mounted [#](https://vuejs.org/v2/api/#mounted) |
224 | `beforeUpdate` | When data changes, before the DOM is patched [#](https://vuejs.org/v2/api/#beforeUpdate) |
225 | `updated` | After a data change [#](https://vuejs.org/v2/api/#updated) |
226 | `beforeDestroy` | Before the instance is destroyed [#](https://vuejs.org/v2/api/#beforeDestroy) |
227 | `destroyed` | After a Vue instance has been destroyed [#](https://vuejs.org/v2/api/#destroyed) |
229 See: [Lifecycle Hooks](https://vuejs.org/v2/api/#Options-Lifecycle-Hooks)
233 #### Set listener on component, within its parent
235 <button-counter v-on:incrementBy="incWithVal">
238 #### Inside parent component
241 incWithVal: function (toAdd) { ... }
245 #### Inside button-counter template
248 'incrementBy', // Custom event name
249 5 // Data sent up to parent
253 Use props to pass data into child components,
254 custom events to pass data to parent elements.
256 See: [Custom Events](https://vuejs.org/v2/guide/components-custom-events.html)
258 Single file components
264 <p>{{ greeting }} World!</p>
285 See: [Single File Components](https://vuejs.org/v2/guide/single-file-components.html)
290 <div>This will be pre-compiled</div>
292 <script src="./my-component.js"></script>
293 <style src="./my-component.css"></style>
296 See: [What About Separation of Concerns?](https://vuejs.org/v2/guide/single-file-components.html#What-About-Separation-of-Concerns)
301 ### Using a single slot
303 #### Component template
308 Only displayed if no slot content
312 {: data-line="3,4,5"}
314 #### Use of component with data for slot
317 <p>This will go in the slot</p>
322 See: [Slots](https://vuejs.org/v2/guide/components-slots.html)
326 #### Component template
328 <div class="container">
330 <slot name="header"></slot>
333 <slot>Default content</slot>
336 <slot name="footer"></slot>
340 {: data-line="3,6,9"}
342 #### Use of component with data for slots
345 <h1 slot="header">Page title</h1>
346 <p>the main content.</p>
347 <p slot="footer">Contact info</p>
350 {: data-line="2,3,4"}
352 See: [Slots](https://vuejs.org/v2/guide/components-slots.html)
357 * [Vue CLI](https://cli.vuejs.org/) _(cli.vuejs.org)_
358 * [Vue Router](https://router.vuejs.org/) _(router.vuejs.org)_
359 * [Vue DevTools](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en) _(chrome.google.com)_
360 * [Nuxt.js](https://nuxtjs.org/) _(nuxtjs.org)_
361 * [Vue.js v1.0.28 cheatsheet](vue@1.0.28/) _Legacy version_