The Point of
Exploring JavaScript Frameworks
The Point of Holly Schinsky devgirlfl devgirl.org Vue.js is The - - PowerPoint PPT Presentation
Exploring JavaScript Frameworks The Point of Holly Schinsky devgirlfl devgirl.org Vue.js is The Progressive JavaScript Framework Creator - Evan You Initial release in Feb 2014 Previously worked at Google and Meteor W orks on Vue full
Exploring JavaScript Frameworks
devgirlfl devgirl.org
Initial release in Feb 2014 Previously worked at Google and Meteor W
Funded by the Patreon campaign
“…what if I could just extract the part that I realmy liked about Angular and build something realmy lightweight without alm the extra concepts involved?”
Approachable Scalable … makes developers happy :) Productive
Reactive Interfaces Declarative Rendering Data Binding Directives Template Logic Components Event Handling Computed Properties CSS T ransitions and Animations Filters
See vuejs.org for full details
Apps are made up of nested and reusable components
<div id="app"> <app-nav></app-nav> <app-view> <app-sidebar></app-sidebar> <app-content></app-content> </app-view> </div>
var demo = new Vue({ el: '#demo', data: { message: ‘Vue is the best!’ } }) <div id="demo"> <p>{{ message }}</p> <input v-model="message"> </div>
<html> <head> <title>My VueJs App</title> </head> <body> <!-- Container for Vue instance to manage --> <div id="app"> </div> </body> </html> <!-- include Vue.js in our page --> <script src=“https://unpkg.com/vue”></script> <script> // create a new Vue instance mounted to app var vm = new Vue({ el: '#app' }); </script>
index.html
<html> <body> //.. <!-- Container for Vue instance to manage --> <div id=“app”> </div> <script src=“https://unpkg.com/vue”></script> <script> // create a new Vue instance and mount it to app div var vm = new Vue({ el: '#app' }); </script> //… <template id="hello-template"> <div> <h1>Hello World!</h1> </div> </template> // Register the hello component Vue.component('hello', { template: '#hello-template' }); <hello></hello>
index.html
Vue.component('hello', { template: '#hello-template', // ... }) new Vue({ el: '#app', // ... }
Global
var Goodbye = { template: '<div> Goodbye World!</div>' } Vue.component('hello', { template: '#hello-template', components: { 'Goodbye': Goodbye } });
Local
import Hello from './components/Hello' export default { name: 'app', components: { Hello }, // ... }
Local by Module
Defined before root app instance
<template> <div class="hello"> <h1>{{ msg }}</h1> </div> </template> <script> export default { name: 'hello', data () { return { msg: 'Welcome to Your Vue.js App' } </script> <style scoped> h1, h2 { font-weight: normal; } .hello { color: magenta; } </style>
hello.vue
<h1>Msg: {{ msg.split('').reverse().join('') }}</h1>
Support JavaScript expressions
<h1>Message: {{ msg }}</h1>
Mustache-like syntax for basic text interpolation (like Angular) Use v-bind directive (or : prefix) within an HTML attribute
<button :disabled="isButtonDisabled">Go</button> <div v-bind:id="dynamicId"></div>
Vue.component('demo', { template: ‘#demo-template', data: function () { return { fname: 'Holly', lname: 'Schinsky', isDisabled: false, items: ['Milk','Bread','Cereal','Peanut Butter'], counter: 0 } },
created
<input type="text" v-model="message"/>
Vue.component('hello', { template: '#hello-template', props: ['message'], data: function () { return { fname: 'Holly', lname: 'Schinsky', } } })
<hello v-bind:message=“message"></hello> <h1>{{ message }}</h1> hello component uses parent component passes
<button @click="addToCount">Add 1</button> <button v-on:click="addMore(4, $event)">Add 4</button>
Use v-on or @ prefix (shorthand)
<input v-on:keyup.enter="submit"> <a v-on:click.stop="doThis"></a>
Modifiers restrict handling to certain specific events
v-text v-html v-show v-if v-else v-else-if v-for v-on v-bind v-model v-pre v-cloak v-once
<input type="text" v-model="message"/> <li v-for="item in items"> {{ item }} </li> <button v-on:click="counter+=1">Add 1</button> <textarea v-bind:disabled='isDisabled' v-bind:class='{ disabled: isDisabled }'>{{ taVal }} </textarea> <h1 v-if="isWinner">Winner winner chicken dinner!</h1> <h1 v-else>Sorry about your luck</h1>
<!-- in text interpolation --> {{ message | capitalize }} <!-- in v-bind --> <div v-bind:id="rawId | formatId"></div>
Use the pipe | symbol to add a filter function: Define filter function in instance options:
new Vue({ // … filters: { capitalize: function (value) { if (!value) return '' return value.toString().toUpperCase(); } } })
things that don’t require reactive properties
Vue.component('demo', { template: ‘#demo-template', //.. computed: { fullName: function () { return this.fname + ' ' + this.lname; } }
new Vue({ data: { a: 1 }, created: function () { console.log('a is: ' + this.a) } }) // => "a is: 1" created(), mounted(), updated(), destroyed() etc
Run code at different stages of a Vue component lifecycle
https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks
$ npm install -g vue-cli $ vue init <template-name> <project-name> $ vue init webpack my-vue-app
etc)
import { Component } from '@angular/core'; @Component ({ selector: 'my-app', template: `<h1>Hello {{name}}</h1>`, }) export class AppComponent { name = 'Angular'; } var helloApp = angular.module("helloApp", []); helloApp.controller("HelloCtrl", function($scope) { $scope.name = "Calvin Hobbes"; }); <div id="app"> {{ message }} </div> var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } })
Angular 1 Angular 2 Vue
var HelloMessage = React.createClass({ render: function () { return <h1>Hello {this.props.message}!</h1>; } }); ReactDOM.render(<HelloMessage message="World" />, document.getElementById('root')); <div id="app"> {{ message }} </div> var app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } })
React Vue
Small file size of Vue.js lends itself well to mobile Several UI libraries available with bindings Framework7 - https://framework7.io/vue/ Onsen UI - https://onsen.io/vue/ Get started quickly using PhoneGap starter project templates - https://github.com/phonegap/phonegap-app- stockpile
vuex - state management - https://vuex.vuejs.org vue-router - routing between views in your SPA - https://router.vuejs.org/en/ axios - HTTP Request library - https://github.com/ axios/axios
https://css-tricks.com/intro-to-vue-1-rendering-directives-events/ https://github.com/vuejs/awesome-vue https://madewithvuejs.com/ https://alligator.io/vuejs/component-lifecycle/ https://vuejs.org/2016/02/06/common-gotchas/ https://github.com/chrisvfritz/vue-2.0-simple-routing-example