├── LICENSE
├── README.md
└── convert.vue
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Matej
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vue-cheat-sheet
2 | My cheat sheet for vue.js most basic stuff. The goal wasn't to make another [Vue documentation](https://vuejs.org/v2/guide/), because the official one is already badass.
3 | Big thank you to [boussadjra](https://github.com/boussadjra/) for making this cheat sheet available as a [website](https://boussadjra.github.io/vue-cheat-sheet/)
4 |
5 | Contributions and PRs are very welcome.
6 |
7 | _"You must type each of these exercises in, manually. If you copy and paste, you might as well not even do them. The point of these exercises is to train your hands, your brain, and your mind in how to read, write, and see code. If you copy-paste, you are cheating yourself out of the effectiveness of the lessons."_ - Zed A.
8 |
9 | Sources:
10 | * [iamshaunjp](https://github.com/iamshaunjp/vuejs-playlist)
11 | * [Vue.js official guide](https://vuejs.org/v2/guide/)
12 |
13 | Useful Chrome extensions:
14 | * [Vue Devtools](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en)
15 | * [JSON Formatter](https://chrome.google.com/webstore/detail/json-formatter/bcjindcccaagfpapjjmafapmmgkkhgoa?hl=en)
16 |
17 | Stuff that might get handy in almost every Vue.js project:
18 | * [Auth restrictions](http://www.eddyella.com/2017/04/24/vue-2-spa-restricting-components-for-logged-in-users-only/)
19 | * [Vue reactivity](https://vuejs.org/v2/guide/reactivity.html)
20 | * [Improve Vuex performance](https://medium.com/@jiihu/how-to-improve-performance-of-vuex-store-c9e3cfb01f72)
21 |
22 | ---
23 | ## Basic HTML and JS
24 | ```html
25 |
26 |
40 |
41 |
42 |
43 |
44 | ```
45 | ```javascript
46 | new Vue({
47 | el: '#vue-app', // contoled element
48 |
49 | data: {
50 | name: "Matej",
51 | age: 27,
52 | sleepy: true
53 | },
54 |
55 | methods: {
56 | hello: function () {
57 | return "Hello";
58 | },
59 | computed:{}
60 | }
61 | });
62 | ```
63 | 
64 |
65 | ## HTML directives
66 | ##### Show / hide div
67 | ##### Hides the element (display none), doesn't delete it
68 | where _available_ is a boolean variable in the js
69 | ```html
70 |
Stuff
71 | ```
72 | ##### Toggle show / hide div
73 | where _available_ is a boolean variable in the js
74 | ```html
75 |
Stuff
76 | ```
77 |
78 | ##### Render div
79 | ##### Deletes the element, doesn't hide it
80 | where _available_ is a boolean variable in the js
81 | ```html
82 |
Stuff
83 |
Smth else
84 | ```
85 |
86 | ##### Looping
87 | ##### array of strings
88 | Remember to check if the element exists with v-if before looping over it
89 | ```html
90 |
126 | ```
127 |
128 | ##### Set text for element from a variable _name_
129 | ```html
130 |
131 | ```
132 | ##### Set html for element from a variable _name_
133 | ```html
134 |
135 | ```
136 |
137 | ## Two way data binding
138 | ```html
139 |
140 |
My name is: {{name}}
141 | ```
142 | ```javascript
143 | ...
144 | data:{
145 | name: ""
146 | }
147 | ...
148 | ```
149 |
150 | ## Computed properties
151 | > Computed properties are cached, and only re-computed on reactive dependency changes. Note that if a certain dependency is out of the instance’s scope (i.e. not reactive), the computed property will not be updated. In other words, imagine a computed property as a method (but it's not really a method) in the ```data()``` that always returns a value. That "method" will be called whenever a property (variable from ```data()```) used in that method is changed.
152 |
153 | ```html
154 |
155 |
156 |
157 | VueJS example
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
Counter 1: {{ a }}
167 |
Counter 2: {{ b }}
168 |
169 |
170 |
Result: {{ result() }} | {{ output }}
171 |
172 |
173 |
174 |
175 |
176 |
177 | ```
178 |
179 | ```javascript
180 | new Vue({
181 | el: '#vue-app',
182 | data: {
183 | a: 0,
184 | b: 0
185 | },
186 | methods: {
187 | result: function () {
188 | // this function is not interested in the "b" variable, yet it runs every time when the result needs to be changed
189 | console.log("methods");
190 | return this.a < 0 ? "Negative" : "Positive";
191 | }
192 | },
193 | computed: {
194 | // these methods are invoked like attributes, without ()
195 | // this method runs only when the "a" variable is changed
196 | output: function () {
197 | console.log("computed");
198 | return this.a < 0 ? "Negative" : "Positive";
199 | }
200 | }
201 | });
202 | ```
203 | ##### Computed property methods can also have getters and setters
204 | ```javascript
205 | var vm = new Vue({
206 | data: { a: 1 },
207 | computed: {
208 | // get only
209 | aDouble: function () {
210 | return this.a * 2
211 | },
212 | // both get and set
213 | aPlus: {
214 | get: function () {
215 | return this.a + 1
216 | },
217 | set: function (v) {
218 | this.a = v - 1
219 | }
220 | }
221 | }
222 | })
223 | vm.aPlus // => 2
224 | vm.aPlus = 3
225 | vm.a // => 2
226 | vm.aDouble // => 4
227 | ```
228 |
229 | ## HTML properties and classes
230 | ```html
231 |
...
232 | ```
233 | this div will have the _red_ class if the _userFound_ variable is set to _true_
234 | ```html
235 |
...
236 | ```
237 | this div will have the _red_ class if the _isAdmin_ variable is set to _true_
238 | ```html
239 |
...
240 | ```
241 |
242 | ## Events
243 | ##### Call _method_ on click event
244 | where _method_ is a custom method in the js
245 | ```html
246 |
247 | ```
248 | ##### or shorthand
249 | where _method_ is a custom method in the js
250 | ```html
251 |
252 | ```
253 | _method_ is called when ALT+ENTER is pressed
254 | ```html
255 |
256 | ```
257 | ##### Conditional event binding (as of Vue 2.6)
258 | The method ```sendModey``` will be called only if the condition ``` amount > 0 ``` has been met.
259 | ```vue
260 |
261 | ```
262 |
263 | ## Custom events
264 | ```javascript
265 | // fire custom event
266 | this.$emit("eventName", data);
267 | ```
268 | ```html
269 |
273 |
274 | ```
275 |
276 | ## Event bus
277 | ##### communicate between child components without the parent component
278 | ##### consider using Vuex instead
279 | ```javascript
280 | // main.js
281 | // create new event bus
282 | export const bus = new Vue();
283 | ```
284 | ```html
285 | // Header.vue
286 | import {bus} from "../main";
287 | ```
288 | ```html
289 | // Footer.vue
290 | import {bus} from "../main";
291 | ```
292 | ```javascript
293 | // listen to bus event in first component
294 | // usually in .created() function
295 | bus.$on("eventName", (data) => {
296 | // callback
297 | // use data
298 | })
299 |
300 | // fire bus event in second component
301 | bus.$emit("eventName", data);
302 | ```
303 |
304 | ## Components
305 | ##### reusable inside the html
306 | ```html
307 |
460 | ```
461 | ```javascript
462 | // main.js
463 | Vue.filter("to-uppercase", function ( value ) {
464 | return value.toUpperCase();
465 | });
466 | ```
467 |
468 | ## Mixins
469 | ##### Reuse some piece if code (or function) so that it doesn't need to be written in more separate files.
470 |
471 |
472 | ## References
473 | ##### An object of DOM elements and component instances
474 | ```html
475 |
476 | ```
477 | ```javascript
478 | var name = this.$refs.name;
479 |
480 | ```
481 |
482 | ## Dynamic components
483 | dynamically change component based on variable _component_ value
484 | rememberto use _keep-alive_ tag to remember data from the destroyed component
485 | ```vue
486 |
487 |
488 |
489 | v-bind:is="componentName">
490 |
491 |
492 |
493 |
494 | import formOne from "./components/formOne.vue";
495 | import formTwo from "./components/formTwo.vue";
496 |
497 | ...
498 | data: function() {
499 | return {
500 | component: "form-two"
501 | }
502 | }
503 | ```
504 |
505 | ## Vue CLI
506 | ##### make new project
507 | ```
508 | $ vue init webpack-simple my-project
509 | $ cd project-name
510 | ```
511 | ##### install dependencies and start local server
512 | ```
513 | $ npm install
514 | $ npm run dev
515 | ```
516 | ##### build app for production
517 | this will make a dist folder with minified js
518 | ```
519 | $ npm run build
520 | ```
521 |
522 | ## Vue lifecycle
523 | * new Vue();
524 | * .beforeCreate();
525 | * .created();
526 | * .beforeMount();
527 | * .updated();
528 | * .beforeUpdate();
529 | * .beforeDestroy();
530 | * .destroyed();
531 | 
532 |
533 | ## Checkboxes
534 | ##### with v-model, the _categories_ array will be appended with the values
535 | ```html
536 |