Vue.js 101

A presentation at Fluent in June 2018 in San Jose, CA, USA by Ben Hong

Slide 1

Slide 1

Vue.js 101 Ben Hong Senior UI Engineer at Politico @bencodezen Be sure to clone the repo if you want to follow along and/or work on the exercises! Repo: https://github.com/bencodezen/vuejs-101-tutorial Slides: https://slides.com/bencodezen/vuejs-101-tutorial/live

Slide 2

Slide 2

Before we get started... You can find the resources below: Repo: https://github.com/bencodezen/vuejs-101-tutorial Slides: https://slides.com/bencodezen/vuejs-101-tutorial And for those posting on social media: #vuejs101

Slide 3

Slide 3

Ben Hong Senior UI Developer @ POLITICO @bencodezen

Slide 4

Slide 4

VueDC https://www.vuedc.io Ben Hong Senior UI Developer @ POLITICO @bencodezen

Slide 5

Slide 5

VueDC https://www.vuedc.io Ben Hong Senior UI Developer @ POLITICO @bencodezen VueMeetups https://www.vuemeetups.org

Slide 6

Slide 6

A Little About You You... ...

Slide 7

Slide 7

Slide 8

Slide 8

Learning Format

  1. Learn 2. Reinforce 3. Apply

Slide 9

Slide 9

Learning Format Concepts 1. Learn 2. Reinforce 3. Apply Examples

Slide 10

Slide 10

Learning Format Concepts 1. Learn 2. Reinforce 3. Apply Examples Questions Clarification

Slide 11

Slide 11

Learning Format Concepts 1. Learn 2. Reinforce 3. Apply Examples Questions Clarification Practice Experiment Get Help

Slide 12

Slide 12

Participation Guidelines 1. Raise your hand for questions at any time! 2. All examples are public (no need to copy down code examples) 3. Please no recording (for the privacy of participants)

Slide 13

Slide 13

Q&A

Slide 14

Slide 14

A Short Introduction Vue.js

Slide 15

Slide 15

Vue is a progressive framework for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. adoptable.

Slide 16

Slide 16

Why use client-side frameworks?

Slide 17

Slide 17

Allows you to control every aspect of your site's user experience

Slide 18

Slide 18

®

Slide 19

Slide 19

Slide 20

Slide 20

Most Popular Client-Side Frameworks

Slide 21

Slide 21

Angular Overview

Slide 22

Slide 22

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

Slide 23

Slide 23

Angular Overview Developed and maintained by Google Easy to get something up and running pretty quickly Most of what was happening seemed more like magic Opinionated on how you should build your app

Slide 24

Slide 24

React Overview

Slide 25

Slide 25

React Overview Large community base and has a model for cross-platform development Great performance due to the use of the virtual DOM You got a lot better at vanilla JavaScript and ES6 very quickly

Slide 26

Slide 26

React Overview Large community base and has a model for cross-platform development Great performance due to the use of the virtual DOM You got a lot better at vanilla JavaScript and ES6 very quickly There is a high learning curve to simply get started Unfriendly to developers who are not well versed in JavaScript ES6+ It's a bit like the Wild West as far as how things should be done

Slide 27

Slide 27

Vue Overview

Slide 28

Slide 28

Vue Overview 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 29

Slide 29

Vue Overview 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 Does not currently have a formal model for cross-platform development

Slide 30

Slide 30

Which framework should you choose?

Slide 31

Slide 31

It depends depends... ...

Slide 32

Slide 32

Choose Vue

Slide 33

Slide 33

Vue is the most compassionate framework in the market right now because it allows you to choose what's best for you. you. Ben Hong

Slide 34

Slide 34

The Basics of Vue.js

Slide 35

Slide 35

A Vue Instance

Slide 36

Slide 36

Declarative Rendering

Slide 37

Slide 37

Exercise #1: #1: Vue Basics Convert a flat HTML file into a basic Vue app Instructions Extra Credit

  1. Open 01-captain-marvel.html
  2. Refactor data model to accommodate the
  3. Create a new Vue instance 3. Attach to the correct HTML element 4. Extract the following data into the app: Hero Name Real Name Height, Eye and Hair Color Citizenship Place of Birth Powers Abilities table format 2. Refactor abilities to use an Array of snippets to improve reuse 3. Add a new data property 'Gender' to the app and use it to determine the pronoun being used in the text 4. Add a new data property 'Location' that generates a random longitude and latitude each time the page is reloaded

Slide 38

Slide 38

Let's Talk About Directives

Slide 39

Slide 39

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

Slide 40

Slide 40

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­for v­else v­bind v­else­if v­on v­show v­model

Slide 41

Slide 41

v-if v-else v-else-if “ listeners and child It ensures that event components inside the conditional block are properly destroyed and recreated during toggles.

Slide 42

Slide 42

v-show “ initial condition, with CSS-based toggling. The element is always rendered regardless of

Slide 43

Slide 43

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

Slide 44

Slide 44

v-if v-else v-else-if Higher toggle costs v-show Higher initial cost

Slide 45

Slide 45

v-if v-else v-else-if v-show Higher toggle costs Higher initial cost It's lazy, so it only renders Renders on the page when the condition is true regardless

Slide 46

Slide 46

v-if v-else v-else-if v-show Higher toggle costs Higher initial cost It's lazy, so it only renders Renders on the page when the condition is true regardless Ensures event listeners Uses CSS to toggle the and child components are display of the element properly destroyed

Slide 47

Slide 47

Slide 48

Slide 48

v-for Allows us to "render a list of items based on an array [or object]."

Slide 49

Slide 49

v-bind Allow us to manipulate HTML attributes with dynamic data

Slide 50

Slide 50

v-bind Allow us to manipulate HTML attributes with dynamic data

Slide 51

Slide 51

v-on Allow us to attach JavaScript functions to common events

Slide 52

Slide 52

v-on Allow us to attach JavaScript functions to common events

Slide 53

Slide 53

v-on Common DOM events that you most likely be using a fair amount @click @keyup @keydown @input @change @submit

Slide 54

Slide 54

v-on Modifiers are a syntactic sugar to help with common functionality

Slide 55

Slide 55

v-model Allows us to use two-way data binding

Slide 56

Slide 56

Q&A

Slide 57

Slide 57

Exercise #2: #2: Counter Build a counter app Instructions Extra Credit

  1. Open 02-counter-app.html
  2. Add the ability to reset the count data
  3. Create a new Vue instance
  4. Allow the user to dynamically set the
  5. Attach to the correct HTML element amount that the counter is incremented
  6. Functionality: or decremented by Render dynamic count data Add ability to increment count Add ability to decrement count
  7. Allow the user to save a snapshot of the current count and restore it if desired 4. Allow the user to generate a list of snapshots that can be restored at any point

Slide 58

Slide 58

Quick Break! Repo: https://github.com/bencodezen/vuejs-101-tutorial Slides: https://slides.com/bencodezen/vuejs-101-tutorial

Slide 59

Slide 59

Quick Break! Repo: https://github.com/bencodezen/vuejs-101-tutorial Slides: https://slides.com/bencodezen/vuejs-101-tutorial Please fill out this quick survey! https://bencodezen.typeform.com/to/Q1pcsZ

Slide 60

Slide 60

Quick Break! Repo: https://github.com/bencodezen/vuejs-101-tutorial Slides: https://slides.com/bencodezen/vuejs-101-tutorial Please fill out this quick survey! https://bencodezen.typeform.com/to/Q1pcsZ

Slide 61

Slide 61

Quick Debrief

Slide 62

Slide 62

Quick Debrief A little more about you...

Slide 63

Slide 63

Quick Debrief A little more about you... There are many ways to accomplish the things we are doing in this tutorial

Slide 64

Slide 64

Let's talk about Vue.js CLI applications

Slide 65

Slide 65

https://cli.vuejs.org/

Slide 66

Slide 66

Slide 67

Slide 67

Vue.js App Tour

Slide 68

Slide 68

Single File Component *.vue

Slide 69

Slide 69

Q&A

Slide 70

Slide 70

Exercise #3: #3: Intro to SFCs Migrate your Counter app into the CLI Instructions Extra Credit

  1. Open App.vue
  2. Migrate the CSS over to the app
  3. Copy over your: Template Vue Instance

Slide 71

Slide 71

If you think SFCs are cool now... now...

Slide 72

Slide 72

Slide 73

Slide 73

Slide 74

Slide 74

Slide 75

Slide 75

Slide 76

Slide 76

Exercise #4: #4: Create a SFC Refactor out your counter app to a SFC! Instructions 1. Create a SFC in the /component directory called Counter.vue 2. Migrate all Counter properties to this new SFC 1. Template 2. Data 3. Methods 4. CSS (Optional) 3. Import your component into App.vue so that your component renders on the page!

Slide 77

Slide 77

You can pass data to your SFCs!

Slide 78

Slide 78

Slide 79

Slide 79

And just when you thought SFCs could not be cooler...

Slide 80

Slide 80

You can use directives on SFCs too! ノ ヮ ノ ・゚✧ ( ◕ ◕) *:

Slide 81

Slide 81

Exercise #5: #5: Sign Up Form Convert flat web page file to a SFC Instructions Extra Credit

  1. Locate 05-sign-up.html and 05-sign-up.css
  2. Add additional password logic for the template and styles 2. Create a new component called AccountCreation.vue and import it into App.vue 3. Functionality to build: Toggle visibility of error messages based on criteria per input field Make Submit button dynamically disable based on login
  3. Create password verification functionality with the appropriate error toggling ability 3. Refactor the UI to reduce repetition of HTML 4. Dynamically style the input fields based on whether error or success

Slide 82

Slide 82

Vue.js has its own DevTools extension...

Slide 83

Slide 83

Exercise #6: #6: To Do App Build a to do list app from scratch Instructions Extra Credit

  1. Create a new Todo component called
  2. Refactor HTML into single file components Todo.vue 2. Import the component into the page and make sure it renders: 3. Basic Functionality App should render a list of tasks User should be able to add new tasks User should be able to complete tasks Dynamically style tasks that are completed User should be able to delete tasks as you see fit to reduce clutter and increase reuse 2. Create a "Trash Can" list that keeps the items the user has "deleted" so that they can undo the deletion 3. Add "Due Date" property to tasks 4. Dynamically style the task if it is overdue 5. Add "Tags" property to tasks that allow you to sort and filter your tasks

Slide 84

Slide 84

So let's do a quick revue re vue... ...

Slide 85

Slide 85

What We Covered Today Concepts Basics of Vue.js Declarative Rendering Data Store Directives Methods Vue CLI Single File Components Vue DevTools Built three apps 1. Counter 2. Sign Up Form UI 3. To Do List

Slide 86

Slide 86

Q&A

Slide 87

Slide 87

Congratulations! You are ready to build and work on Vue.js applications!

Slide 88

Slide 88

But wait, there's more more!!

Slide 89

Slide 89

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 Lifecycle Methods Popular Vue.js Tools State Management Custom Directives Routing Animations vuex vetur Vue DevTools

Slide 90

Slide 90

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

Slide 91

Slide 91

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/ Vue.js Meetups https://www.vuemeetups.org

Slide 92

Slide 92

Vue is the most compassionate framework in the market right now because it allows you to choose what's best for you. you. Ben Hong

Slide 93

Slide 93

Thank you! If you have any additional questions, please feel free to reach out to me. @bencodezen