Guest User

Untitled

a guest
Jan 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. # Expressions
  2.  
  3. ```html
  4. <h1>{{ expression }}</h1>
  5. ```
  6.  
  7. # Binding
  8.  
  9. ```html
  10. <img v-bind:src="expression" />
  11. ```
  12.  
  13. ### Shorthand
  14.  
  15. ```html
  16. <img :src="expression" />
  17. ```
  18.  
  19. ### Some other common bindings
  20.  
  21. ```html
  22. <div :alt></div>
  23. <div :href></div>
  24. <div :title></div>
  25.  
  26. <div :class></div>
  27. <div :disabled></div>
  28. ```
  29.  
  30. # Events
  31.  
  32. Some generic events
  33.  
  34. ```html
  35. <button v-on:click="addToCart">Add to Cart</button>
  36. <button @click="addToCart">Add to Cart</button>
  37. <div @mouseover="updateProduct">Color</div>
  38. <form @submit="addToCart">Color</form>
  39. <input @keyup.enter="send">...</input>
  40. ```
  41.  
  42. # Loops
  43.  
  44. ```html
  45. <div v-for="variant in variants"
  46. :key="variant.variantId"></div>
  47. ```
  48.  
  49. # Class & Style binding
  50.  
  51. ```html
  52. <div :style = { backgroundColor: product.color }
  53. @mouseover="updateProduct(product.image)"></div>
  54.  
  55. <button @click="addToCart"
  56. :disabled="!inStock"
  57. :class="{ disabledButton: !inStock }">Add to Cart</button>
  58. ```
  59.  
  60. # Computed values
  61.  
  62. **Computed values are cached**
  63.  
  64. ```js
  65. var app = new Vue({
  66. el: '#app',
  67. data: {
  68. product: 'Socks',
  69. brand: 'Adidas',
  70. cart: 0
  71. },
  72. methods: {
  73. addToCart: function() {
  74. this.cart += 1;
  75. }
  76. },
  77. computed: {
  78. title() {
  79. return this.brand + ' ' + this.product;
  80. }
  81. }
  82. });
  83. ```
  84.  
  85. # Components
  86.  
  87. Similar to app object. Should contain one element.
  88.  
  89. ```js
  90. Vue.component('product', {
  91. props: {
  92. message: {
  93. type: String,
  94. required: true,
  95. default: 'Hi'
  96. }
  97. },
  98. template: `<h1>{{ message }}</h1>`,
  99. data() {
  100. return {
  101. }
  102. }
  103. });
  104. ```
  105.  
  106. this component produces HTML below:
  107.  
  108. ```html
  109. <product message="hello world!"></product>
  110. ```
  111.  
  112. # Communication events
  113.  
  114. Add method into a component to $emit event message:
  115.  
  116. ```js
  117. methods: {
  118. addToCart: function() {
  119. this.$emit('add-to-cart');
  120. }
  121. }
  122. ```
  123.  
  124. Then add event attribute to component in template:
  125.  
  126. ```html
  127. <product premium="premium" @add-to-cart="updateCart"></product>
  128. ```
  129.  
  130. And handle event in parent:
  131. ```js
  132. methods: {
  133. updateCart() {
  134. this.cart += 1;
  135. }
  136. }
  137. ```
  138.  
  139. # Forms
  140.  
  141. Two way data binding.
  142.  
  143. Create new component as below:
  144.  
  145. ```js
  146. Vue.component('product-review', {
  147. template:`
  148. <form class="review-form" @submit.prevent="onSubmit">
  149. <input v-model="name">
  150. <input v-model="review">
  151. <select v-model.number="rating">
  152. <option>5</option>
  153. <option>4</option>
  154. <option>3</option>
  155. <option>2</option>
  156. <option>1</option>
  157. </select>
  158. <button type="submit">Submit</button>
  159. </form>
  160. `,
  161. data() {
  162. return {
  163. name: null,
  164. review: null,
  165. rating: null
  166. }
  167. },
  168. methods: {
  169. onSubmit() {
  170. let productReview = {
  171. name: this.name,
  172. review: this.review,
  173. rating: this.rating
  174. };
  175. this.$emit('review-submitted', productReview);
  176. this.name = null;
  177. this.review = null;
  178. this.rating = null;
  179. }
  180. }
  181. })
  182. ```
  183.  
  184. And add new component to product as shown below:
  185.  
  186. ```html
  187. <product>
  188. <product-review @review-submitted="addReview"></product-review>
  189. </product>
  190. ```
Add Comment
Please, Sign In to add comment