Vue.js: The Compassionate Framework

A presentation at Cream City Code in October 2019 in Milwaukee, WI, USA by Ben Hong

Slide 1

Slide 1

VUE.JS The Compassionate Framework Ben Hong (@bencodezen) Cream City Code 2019

Slide 2

Slide 2

A little bit about me…

Slide 3

Slide 3

Slide 4

Slide 4

Google Developer Expert Mozilla TechSpeaker BEN HONG Senior Frontend Engineer Meltano in GitLab @bencodezen Vue.js Community Partner Vue Mastery Instructor VuePress Core Team

Slide 5

Slide 5

Before we get started… All resources will be available online https://www.twitter.com/bencodezen For the social media folks @bencodezen - #CreamCityCode - #FallExperiment

Slide 6

Slide 6

What is Vue.js?

Slide 7

Slide 7

Vue.js is a progressive JavaScript framework

  1. Approachable 2. Versatile 3. Performant

Slide 8

Slide 8

Slide 9

Slide 9

Slide 10

Slide 10

Slide 11

Slide 11

PROTEIN VEG CARBS SAUCE TOPPINGS

Slide 12

Slide 12

? TOPPINGS PROTEIN CARBS SAUCE VEG

Slide 13

Slide 13

?

Slide 14

Slide 14

Slide 15

Slide 15

Slide 16

Slide 16

Slide 17

Slide 17

Slide 18

Slide 18

Slide 19

Slide 19

Slide 20

Slide 20

Slide 21

Slide 21

Slide 22

Slide 22

Slide 23

Slide 23

Facebook Google Community

Slide 24

Slide 24

Vue.js Fundamentals

Slide 25

Slide 25

An instance of Vue.js const app = new Vue({ el: ‘#app’, data: { firstName: ‘Ben’, lastName: ‘Hong’, event: ‘Cream City Code’ } })

Slide 26

Slide 26

An instance of Vue.js const app = new Vue({ el: ‘#app’, data: { firstName: ‘Ben’, lastName: ‘Hong’, event: ‘Cream City Code’ } }) Uses a CSS selector to determine where this Vue Instance will live.

Slide 27

Slide 27

An instance of Vue.js const app = new Vue({ el: ‘#app’, data: { firstName: ‘Ben’, lastName: ‘Hong’, event: ‘Cream City Code’ } }) A data store for the Vue to access in the app.

Slide 28

Slide 28

Expressions in Vue.js const app = new Vue({ el: ‘#app’, data: { firstName: ‘Ben’, lastName: ‘Hong’, event: ‘Cream City Code’ } }) <!— Using data in your template is easy! —> <div id=”app”> <p>{{ firstName }} {{ lastName }} is at {{ event }}!</p> </div>

Slide 29

Slide 29

Expressions in Vue.js const app = new Vue({ el: ‘#app’, data: { firstName: ‘Ben’, lastName: ‘Hong’, event: ‘Cream City Code’ } }) <!— This is how it renders! —> <div id=”app”> <p>Ben Hong is at Cream City Code!</p> </div>

Slide 30

Slide 30

Expressions in Vue.js <!— Using data in your template is easy! —> <p>{{ firstName }} {{ lastName }} is at {{ conference }}!</p> <!— You can even do math! —> <p>Let’s perform calculations! {{ 12 * 12 / 4 }}</p> <!— Any simple expression just works —> <p>{{ firstName.toUpperCase() }} IS EXCITED!</p> <!— Basic logic works too! —> <p>{{ onSale ? price * 0.8 : price }}</p>

Slide 31

Slide 31

Computed properties

<p>{{ onSale ? price * 0.8 : price }}</p>

Slide 32

Slide 32

Computed properties

<script> const app = new Vue({ el: ‘#app’, data: { price: 100, onSale: false }, computed: { salePrice() { if (this.onSale) { return this.price * 0.8 } else { return this.price } } } }) </script>

Slide 33

Slide 33

Computed properties const app = new Vue({ el: ‘#app’, data: { price: 100, onSale: false }, computed: { salePrice() { if (this.onSale) { return this.price * 0.8 } else { return this.price } <p>{{ onSale ? price * 0.8 : price }}</p> } }

Slide 34

Slide 34

Computed properties const app = new Vue({ el: ‘#app’, data: { price: 100, onSale: false }, computed: { salePrice() { if (this.onSale) { return this.price * 0.8 } else { return this.price } <p>{{ salePrice }}</p> } }

Slide 35

Slide 35

Methods in Vue.js const app = new Vue({ el: ‘#app’, data: { potion: 300, antidote: 100, revive: 1500, onSale: true }, methods: { generatePrice(itemPrice) { if (this.onSale) { return itemPrice * 0.8 } else { return itemPrice } } } })

Slide 36

Slide 36

Let’s talk about directives

Slide 37

Slide 37

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

Slide 38

Slide 38

VUE.JS BASICS: DIRECTIVES WHAT ARE DIRECTIVES? They are Vue specific syntax that flow you to accomplish common goals without worrying about how it is implemented ▸ v-if ▸ v-for ▸ v-else-if ▸ v-on ▸ v-else ▸ v-model ▸ v-bind

Slide 39

Slide 39

DIRECTIVES: V-IF / V-ELSE-IF / V-ELSE V-IF / V-ELSE-IF / V-ELSE Allows you to determine whether an element on the page should exist <article> <h1>Dashboard</h1> <section v-if=”userPermission === ‘admin’”> … </section> <section v-else-if=”userPermission === ‘user’”> … </section> <section v-else> <p>Please login.</p> </section> </article>

Slide 40

Slide 40

DIRECTIVES: V-BIND V-BIND Allows us to manipulate HTML attributes with dynamic data <!— The long form —> <a v-bind:href=”linkUrl”>…</a> <!— The more common shorthand —> <a :href=”linkUrl”>…</a> <!— It will render as… —> <a href=“www.vuejs.org”>…</a>

Slide 41

Slide 41

DIRECTIVES: V-ON V-ON Allows us to attach methods to HTML elements when a certain event occurs <!— The long version —> <button v-on:click=”alert(‘Alohomora’)”>Unlock</button> <!— The more common shorthand —> <button @click=”alert(‘Alohomora’)”>Unlock</button> <!— Built in modifiers! —> <input type=”text” @keyup.enter=“submit” />

Slide 42

Slide 42

DIRECTIVES: V-MODEL V-MODEL Allows us to use two-way binding <div id=”app”> <h1>Live Message Updates!</h1> <p>{{ message }}</p> <input type=”text” v-model=”message”> </div> <script> const app = new Vue({ el: ‘#app’, data: { message: ” } }) </script>

Slide 43

Slide 43

Slide 44

Slide 44

DIRECTIVES: V-FOR V-FOR Allows us to “render a list of items based on an array (or object).” <script> const app = new Vue({ el: ‘#app’, data: { Hogwarts: [ ‘Ravenclaw’, ‘Hufflepuff’, ‘Slytherin’, ‘Gryffindor’ ] } }) </script>

<!— Given the data in the Vue instance —> <div id=”app”> <ul> <li v-for=”house in Hogwarts”> {{ house}} </li> </ul> </div> <!— Will be rendered as… —> <ul> <li>Ravenclaw</li> <li>Hufflepuff</li> <li>Slytherin</li> <li>Gryffindor</li> </ul> </template>

Slide 45

Slide 45

Enough talking. Let’s see Vue.js in action!

Slide 46

Slide 46

What about Vue.js 3.0?

Slide 47

Slide 47

R E What about M I A L C Vue.js 3.0? S I D

Slide 48

Slide 48

Slide 49

Slide 49

No need to wait. Vue.js 3.0 is additive.

Slide 50

Slide 50

Slide 51

Slide 51

Slide 52

Slide 52

Slide 53

Slide 53

DESIGNED TO BE APPROACHABLE TO DEVELOPERS OF PRACTICALLY ALL ABILITIES Photo by Katie Moum on Unsplash

Slide 54

Slide 54

JSX EXAMPLE import React from ‘react’; import { render } from ‘react-dom’; import HelloWorld from ‘./HelloWorld’; const styles = { fontFamily: ‘Avenir, Helvetica, Arial, sans-serif’, textAlign: ‘center’, color: ‘#2c3e50’, marginTop: ‘60px’ }; const App = () => { <div style={styles} className=”app”> <h1>This is a React.js component file!</h1> </div> }; export default App;

Slide 55

Slide 55

SFC EXAMPLE <template> <div> <h1>This is a Vue Single File Component (SFC) file!</h1> </div> </template> <script> export default { name: ‘App’ } </script> <style> #app { font-family: ‘Avenir, Helvetica, Arial, sans-serif’; text-align: center; color: #2c3e50; margin-top: 60px; } </style>

Slide 56

Slide 56

THEIR DOCS ARE ONE OF THE BEST OUT THERE

Slide 57

Slide 57

Vue.js Docs https://vuejs.org/v2/guide/

Slide 58

Slide 58

GIVES YOU WHAT YOU WANT WHEN YOU NEED IT, THEN IT GETS OUT OF YOUR WAY. Paraphrased from Sarah Drasner

Slide 59

Slide 59

Vue.js Style Guide https://vuejs.org/v2/style-guide/

Slide 60

Slide 60

NEXT STEPS… SO WHAT ELSE IS THERE TO LEARN ABOUT? ▸ Props ▸ State Management ▸ Slots ▸ Animations ▸ Watch Property ▸ Render Function ▸ Custom Events ▸ Component Libraries ▸ Lifecycle Hooks ▸ Testing ▸ Managing CSS in Vue.js ▸ And much more!

Slide 61

Slide 61

NEXT STEPS… RECOMMENDED RESOURCES Free ▸ Official Vue.js Guide ▸ VueMastery: Intro to Vue.js ▸ VueSchool Free Courses Paid ▸ Vue Mastery ▸ Vue School ▸ Frontend Masters: Introduction to Vue.js ▸ Udemy: Vue.js 2 - The Complete Guide

Slide 62

Slide 62

THANKS EVERYONE! @bencodezen

Slide 63

Slide 63

Questions?