Vue.js 101 - The Essentials

A presentation at VueDC Meetup in December 2017 in Arlington County, Arlington, VA, USA by Ben Hong

Slide 1

Slide 1

VueDC (@vuejsdc) Wifi: --Password: --GitHub Repo: https://github.com/VueDC/vuejs-101-the-essentials CodePen Collection: https://codepen.io/collection/DmRKRB/ Slides: https://slides.com/bencodezen/vuejs-101-the-essentials/

Slide 2

Slide 2

Announcements

Slide 3

Slide 3

Announcements VueConf.US - http://vueconf.us

Slide 4

Slide 4

Announcements VueConf.US - http://vueconf.us Next Meetup Vue.js 201 - Mid-January 2018

Slide 5

Slide 5

Announcements VueConf.US - http://vueconf.us Next Meetup Vue.js 201 - Mid-January 2018 Interested in speaking?

Slide 6

Slide 6

Announcements VueConf.US - http://vueconf.us Next Meetup Vue.js 201 - Mid-January 2018 Interested in speaking? Reach out to us at vuejsdc@gmail.com

Slide 7

Slide 7

Announcements VueConf.US - http://vueconf.us Next Meetup Vue.js 201 - Mid-January 2018 Interested in speaking? Reach out to us at vuejsdc@gmail.com Sponsor - Politico

Slide 8

Slide 8

Gold Sponsor (Food + Drinks + Venue)

Slide 9

Slide 9

Vue.js 101 The Essentials of Vue.js December 6th, 2017 Ben Hong Senior UI Engineer at Politico @bencodezen Slides: https://slides.com/bencodezen/vuejs-101-the-essentials

Slide 10

Slide 10

What is Vue.js?

Slide 11

Slide 11

®

Slide 12

Slide 12

Slide 13

Slide 13

“ After hand-rolling many [UI prototypes] with vanilla JavaScript and using Angular 1 for a few, I wanted something that captured Evan You (@youyuxi) the declarative nature of Angular's data binding, but with a simpler, more approachable API.

Slide 14

Slide 14

Vue.js

Slide 15

Slide 15

Credit - xkcd: Standards (https://xkcd.com/927/)

Slide 16

Slide 16

What makes Vue.js ? so special special?

Slide 17

Slide 17

Slide 18

Slide 18

Developed and maintained by Google

Slide 19

Slide 19

Developed and maintained by Google Easy to get something up and running pretty quickly

Slide 20

Slide 20

Developed and maintained by Google Easy to get something up and running pretty quickly Most of what was happening seemed more like magic

Slide 21

Slide 21

Developed and maintained by Google Easy to get something up and running pretty quickly Most of what was happening seemed more like magic Larger footprint in web performance

Slide 22

Slide 22

Developed and maintained by Google Easy to get something up and running pretty quickly Most of what was happening seemed more like magic Larger footprint in web performance More opinionated on how you should build your app

Slide 23

Slide 23

Slide 24

Slide 24

Developed by Facebook and recently granted an MIT license

Slide 25

Slide 25

Developed by Facebook and recently granted an MIT license Great performance due to the use of the virtual DOM

Slide 26

Slide 26

Developed by Facebook and recently granted an MIT license Great performance due to the use of the virtual DOM You got a lot better at vanilla JavaScript very quickly

Slide 27

Slide 27

Developed by Facebook and recently granted an MIT license Great performance due to the use of the virtual DOM You got a lot better at vanilla JavaScript very quickly High learning curve even just to get started

Slide 28

Slide 28

Developed by Facebook and recently granted an MIT license Great performance due to the use of the virtual DOM You got a lot better at vanilla JavaScript very quickly High learning curve even just to get started Unfriendly to non-JavaScript developers (i.e., JSX)

Slide 29

Slide 29

Developed by Facebook and recently granted an MIT license Great performance due to the use of the virtual DOM You got a lot better at vanilla JavaScript very quickly High learning curve even just to get started Unfriendly to non-JavaScript developers (i.e., JSX) High pace of development resulting in unexpected bugs

Slide 30

Slide 30

Slide 31

Slide 31

An open-source framework with no corporate influence

Slide 32

Slide 32

An open-source framework with no corporate influence Takes the best of both worlds and brings them together

Slide 33

Slide 33

An open-source framework with no corporate influence Takes the best of both worlds and brings them together It does not alienate non-JavaScript developers

Slide 34

Slide 34

An open-source framework with no corporate influence Takes the best of both worlds and brings them together It does not alienate non-JavaScript developers Great performance that is on par if not better than React.js

Slide 35

Slide 35

An open-source framework with no corporate influence Takes the best of both worlds and brings them together It does not alienate non-JavaScript developers Great performance that is on par if not better than React.js Flexible and accommodating to how you prefer to build apps

Slide 36

Slide 36

An open-source framework with no corporate influence Takes the best of both worlds and brings them together It does not alienate non-JavaScript developers Great performance that is on par if not better than React.js Flexible and accommodating to how you prefer to build apps Smaller team and community compared to React / Angular

Slide 37

Slide 37

Slide 38

Slide 38

The Essentials of Vue.js

Slide 39

Slide 39

Simple Vue.js Setup It's as easy as adding jQuery Start: https://codepen.io/BenCodeZen/pen/rYgGdR Final: https://codepen.io/BenCodeZen/pen/GOarKO

Slide 40

Slide 40

Step 1: Initialize a Vue instance const app = new Vue()

Slide 41

Slide 41

Step 1: Initialize a Vue instance const app = new Vue() Let's pretend we're planning a new trip! const adventure = new Trip()

Slide 42

Slide 42

Mapping Out the Important Details const adventure = new Trip({ destination: 'Diagon Alley', luggage: { name: 'Mr. B. Hong', house: 'Ravenclaw', wand: '12.5" larch wood w/ Dragon heartstring core', wallet: { galleons: 12, sickles: 38, knuts: 274 } }, thingsToDo: { visitGringotts, visitDiagonAlley, drinkButterbeer, eatAtTheLeakyCauldron } })

Slide 43

Slide 43

Step 2: Configure Your "Destination" <!-- Start with an HTML element that will serve as the app's destination --> <div id="diagon-alley"></div> // This is the most important configuration // Mistype this one and nothing will ever happen const adventure = new Vue({ el: '#diagon-alley' })

Slide 44

Slide 44

Step 3: Configure Your "Luggage" // This where all your "luggage" items are stored const adventure = new Vue({ el: '#diagon-alley', data: { name: 'Mr. B. Hong', house: 'Ravenclaw', wand: '12.5" larch wood w/ Dragon heartstring core', wallet: { galleons: 12, sickles: 38, knuts: 274 } } })

Slide 45

Slide 45

Step 4: Configure Your "Things To Do" // This where all your instructions are stored const adventure = new Vue({ el: '#diagon-alley', data: { name: 'Mr. B. Hong', house: 'Ravenclaw', wand: '12.5" larch wood w/ Dragon heartstring core', wallet: { galleons: 12, sickles: 38, knuts: 274 } }, methods: { visitGringotts() { ... }, visitDiagonAlley() { ... }, drinkButterbeer() { ... }, eatAtTheLeakyCauldron() { ... } } })

Slide 46

Slide 46

Let's see it in action! Start: https://codepen.io/BenCodeZen/pen/MOdOjN Final: https://codepen.io/BenCodeZen/pen/MOdoQY

Slide 47

Slide 47

Let's Talk About Directives

Slide 48

Slide 48

Directives are the part of Vue.js that are a bit magical magical... ...

Slide 49

Slide 49

What are directives exactly? They are Vue specific methods that allow you to accomplish common goals without worrying how it is implemented. v-if v-html v-else v-text v-else-if v-pre v-show v-cloak v-for v-once v-bind v-class v-on v-style v-model v-attr

Slide 50

Slide 50

v-if v-else v-else-if v-show

Slide 51

Slide 51

v-if v-else v-else-if v-show “ listeners and child “ rendered regardless of initial components inside the condition, with CSS-based conditional block are toggling. It ensures that event properly destroyed and recreated during toggles." The element is always

Slide 52

Slide 52

Slide 53

Slide 53

v-for Allows us to "render a list of items based on an array [or object]." <!-- Given the data model on the right --> <ul> <li v-for="house in Hogwarts"> {{ house }} </li> </ul> <!-- It will render to --> <ul> <li>Ravenclaw</li> <li>Hufflepuff</li> <li>Slytherin</li> <li>Griffindor</li> </ul> // Given this sample Vue instance new Vue({ ... data: { Hogwarts: [ 'Ravenclaw', 'Hufflepuff', 'Slytherin', 'Gryffindor' ] } })

Slide 54

Slide 54

v-bind Allow us to manipulate HTML attributes with dynamic data <!-- The long form --> <div v-bind:class="{ active: isActive }">...</div> <!-- The common shortcut --> <div :class="{ active: isActive }">...</div> <!-- If isActive is true, it will render --> <div class="active">...</div>

Slide 55

Slide 55

v-on Allow us to attach methods to common events <!-- The long form --> <div v-on:click="alert('Alohomora!')">...</div> <!-- The common shortcut --> <div @click="alert('Alohomora!')">...</div>

Slide 56

Slide 56

v-model Allows us to use two-way data binding <input v-model="name" type="text" id="name" /> <script> new Vue({ ... data: { name: 'Sirius Black' } }) </script>

Slide 57

Slide 57

Quick Break! Final Coding Exercise Start: https://codepen.io/BenCodeZen/pen/YEbEgv Fina: https://codepen.io/BenCodeZen/pen/gXyjwj

Slide 58

Slide 58

Enough talk. Let's write some code! code! Final Coding Exercise Start: https://codepen.io/BenCodeZen/pen/YEbEgv Fina: https://codepen.io/BenCodeZen/pen/gXyjwj

Slide 59

Slide 59

Exercise 1: Dynamic Data 1. Replace 'Static' with the dynamic data property noun 2. Wire up input#noun to dynamically update noun 3. Use dynamic inline-styles to define backgroundImage on div#image-wrapper that uses the data property imageUrl 4. Wire up input#background-image to dynamically update imageUrl

Slide 60

Slide 60

Exercise 2: Directives & Methods 1. Toggle aside#side-menu's visibility when you click on button#toggle-menu 2. Challenge: Write a method so that div#templatewrapper's position is dynamically updated when you click on div#image-wrapper.

Slide 61

Slide 61

“ Gives me what I want when I need it, and then it gets out of my way. - Sarah Drasner (@sarah_edo)

Slide 62

Slide 62

Congratulations! You've worked on three different Vue applications!

Slide 63

Slide 63

But wait... there's more!

Slide 64

Slide 64

Concepts Workflow Computed Properties App Architecture w/ Vue.js Filters Testing with Vue.js Props Managing Styles w/ Vue.js Mixins Animate All Things w/ Vue.js Modifiers Popular Vue.js Tools Lifecycle Methods State Management Custom Directives Routing Animations vue-cli vuex vetur Vue DevTools

Slide 65

Slide 65

Sneak Peak at Vue.js 201

Slide 66

Slide 66

Additional Resources Official Vue.js Docs https://vuejs.org/ FEM: Introduction to Vue.js https://frontendmasters.com/courses/vue/ Udemy: Vue.js Courses https://www.udemy.com/courses/search/?q=vuejs Vue.js Discord Channel https://vue-land.js.org/

Slide 67

Slide 67

Footnotes Evan You's GitHub Story https://github.com/open-source/stories/yyx990803

Slide 68

Slide 68

Thank you! @bencodezen