VueDC

(@vuejsdc)

Wifi: ---

Password: ---

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

Gold Sponsor

(Food + Drinks + Venue)

Vue.js 101

The Essentials of Vue.js

Ben Hong

Senior UI Engineer at Politico

@bencodezen

December 6th, 2017

What is Vue.js?

Evan You (@youyuxi)

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

Vue.js

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

What makes Vue.js so special?

Easy to get something up and running pretty quickly

Most of what was happening seemed more like magic

Larger footprint in web performance

Developed and maintained by Google

More opinionated on how you should build your app

Great performance due to the use of the virtual DOM

High learning curve even just to get started

Unfriendly to non-JavaScript developers (i.e., JSX)

Developed by Facebook and recently granted an MIT license

High pace of development resulting in unexpected bugs

You got a lot better at vanilla JavaScript very quickly

Takes the best of both worlds and brings them together

Great performance that is on par if not better than React.js

Flexible and accommodating to how you prefer to build apps

An open-source framework with no corporate influence

Smaller team and community compared to React / Angular

It does not alienate non-JavaScript developers

The Essentials of Vue.js

Simple Vue.js Setup

It's as easy as adding jQuery



const app = new Vue()

Step 1: Initialize a Vue instance



const adventure = new Trip()

Let's pretend we're planning a new trip!

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
    }
})

Mapping Out the Important Details

<!-- 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'
})

Step 2: Configure Your "Destination"

// 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
        }
    }
})

Step 3: Configure Your "Luggage"

// 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() { ... }
    }
})

Step 4: Configure Your "Things To Do"

Let's see it in action!

Let's Talk About Directives

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

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-else
  • v-else-if
  • v-show
  • v-for
  • v-bind
  • v-on
  • v-model
  • v-html
  • v-text
  • v-pre
  • v-cloak
  • v-once
  • v-class
  • v-style
  • v-attr

v-if

v-else

v-else-if

It ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles."

v-show

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

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'
        ]
    }
})

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>

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>

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>

Quick Break!

Final Coding Exercise

Enough talk.

Let's write some code!

Final Coding Exercise

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

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#template-wrapper's position is dynamically updated when you click on div#image-wrapper.

Gives me what I want when I need it, and then it gets out of my way.

- Sarah Drasner (@sarah_edo)

Congratulations!

You've worked on
three different Vue applications!

But wait... there's more!

Concepts

Workflow

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

Sneak Peak at

Vue.js 201

Additional Resources

Footnotes

Thank you!

VueDC - Vue.js 101

By Ben Hong

VueDC - Vue.js 101

Date: December 6th, 2017 Location: Rosslyn, VA

  • 1,244