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)">...
106 #### To prevent default behavior (e.g. page reload)
108 <form @submit.prevent="addProduct">...
111 #### Only trigger once
113 <img @mouseover.once="showImage">...
116 | Method | Description |
118 | `.stop` | Stop all event propagation |
119 | `.self ` | Only trigger if event.target is element itself |
121 #### Keyboard entry example
123 <input @keyup.enter="submit">
126 #### Call onCopy when control-c is pressed
128 <input @keyup.ctrl.c="onCopy">
131 See: [Events](https://vuejs.org/v2/guide/events.html)
135 #### The `:key` is always recommended
137 <li v-for="item in items"
144 #### To access the position in the array
146 <li v-for="(item, index) in items">...
149 #### To iterate through objects
151 <li v-for="(value, key) in object">...
154 #### Using `v-for` with a component
156 <cart-product v-for="item in products"
161 See: [List Rendering](https://vuejs.org/v2/guide/list.html)
167 ### Component anatomy
170 Vue.component('my-component', {
172 // Components that can be used in the template
177 // The parameters the component accepts
184 validator: function (value) {
185 // Should return true if value is valid
190 // `data` must be a function
197 // Return cached values until dependencies change
198 fullName: function () {
199 return this.firstName + ' ' + this.lastName
203 // Called when firstName changes value
204 firstName: function (value, oldValue) { ... }
207 template: '<span>{{ message }}</span>',
208 // Can also use backticks in `template` for multi-line
211 {: data-line="3, 8, 16, 21, 28, 34, 39"}
213 See: [Components Basics](https://vuejs.org/v2/guide/components.html)
217 | Method | Description |
219 | `beforeCreate` | After the instance has been initialized [#](https://vuejs.org/v2/api/#beforeCreate) |
220 | `created` | After the instance is created [#](https://vuejs.org/v2/api/#created) |
221 | `beforeMount` | Before the first render [#](https://vuejs.org/v2/api/#beforeMount) |
222 | `mounted` | After the instance has been mounted [#](https://vuejs.org/v2/api/#mounted) |
223 | `beforeUpdate` | When data changes, before the DOM is patched [#](https://vuejs.org/v2/api/#beforeUpdate) |
224 | `updated` | After a data change [#](https://vuejs.org/v2/api/#updated) |
225 | `beforeDestroy` | Before the instance is destroyed [#](https://vuejs.org/v2/api/#beforeDestroy) |
226 | `destroyed` | After a Vue instance has been destroyed [#](https://vuejs.org/v2/api/#destroyed) |
228 See: [Lifecycle Hooks](https://vuejs.org/v2/api/#Options-Lifecycle-Hooks)
232 #### Set listener on component, within its parent
234 <button-counter v-on:incrementBy="incWithVal">
237 #### Inside parent component
240 incWithVal: function (toAdd) { ... }
244 #### Inside button-counter template
247 'incrementBy', // Custom event name
248 5 // Data sent up to parent
252 Use props to pass data into child components,
253 custom events to pass data to parent elements.
255 See: [Custom Events](https://vuejs.org/v2/guide/components-custom-events.html)
257 Single file components
263 <p>{{ greeting }} World!</p>
284 See: [Single File Components](https://vuejs.org/v2/guide/single-file-components.html)
289 <div>This will be pre-compiled</div>
291 <script src="./my-component.js"></script>
292 <style src="./my-component.css"></style>
295 See: [What About Separation of Concerns?](https://vuejs.org/v2/guide/single-file-components.html#What-About-Separation-of-Concerns)
300 ### Using a single slot
302 #### Component template
307 Only displayed if no slot content
311 {: data-line="3,4,5"}
313 #### Use of component with data for slot
316 <p>This will go in the slot</p>
321 See: [Slots](https://vuejs.org/v2/guide/components-slots.html)
325 #### Component template
327 <div class="container">
329 <slot name="header"></slot>
332 <slot>Default content</slot>
335 <slot name="footer"></slot>
339 {: data-line="3,6,9"}
341 #### Use of component with data for slots
344 <h1 slot="header">Page title</h1>
345 <p>the main content.</p>
346 <p slot="footer">Contact info</p>
349 {: data-line="2,3,4"}
351 See: [Slots](https://vuejs.org/v2/guide/components-slots.html)
356 * [Vue CLI](https://cli.vuejs.org/) _(cli.vuejs.org)_
357 * [Vue Router](https://router.vuejs.org/) _(router.vuejs.org)_
358 * [Vue DevTools](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en) _(chrome.google.com)_
359 * [Nuxt.js](https://nuxtjs.org/) _(nuxtjs.org)_
360 * [Vue.js v1.0.28 cheatsheet](vue@1.0.28/) _Legacy version_