SlideShare a Scribd company logo
1 of 26
Download to read offline
Introduction to VueJS & Vuex
About me
Bernd Alter
Technical Director at Turbine Kreuzberg
Tech lead, father, boardgame enthusiast, DJ
bernd.alter@turbinekreuzberg.com
@bazoo0815
Agenda
Basics
Bindings
Conditionals & lists
Slots
Routing
Components
State management with Vuex
Tipps & tricks
Basics
Use npm package vue-cli
Easy scaffolding
vue init <template-name> <project-name>
vue init webpack my-vue-app
Basics
Vue instance (single/multiple)
Mustache template syntax
Lifecycle hooks: created, mounted, updated, destroyed
<span>Message: {{ msg }}</span>
<script>
var vm = new Vue({
data () {
return {
msg: 'my message'
}
},
mounted () {
this.$emit('mounted');
}
// more code
})
</script>
Bindings
Attributes: v-bind (shorthand :)
Example:
Result:
<div class="static"
v-bind:class="{ active: isActive, 'danger': hasError }"
:text="dynamicText">
</div>
<script>
data: {
isActive: true,
hasError: false,
dynamicText: 'my dynamic text'
}
</script>
<div class="static active">my dynamic text</div>
Bindings
Events: v-on (shorthand @)
<button v-on:click="onClick">click me</button>
<button @click="onClick">click me</button>
<script>
methods: {
onClick () { // do something on clicking the button }
}
</script>
Bindings
Events: v-on - Modifiers
Key modi ers
use key codes
or more convenient: built-in modi ers
.enter, .tab, .esc, .space, .up, .down, .left, .right, .delete (both 'delete' and
'backspace')
<input v-on:keyup.13="onKeyEnter"></input>
# same as previous
<input @keyup.enter="onKeyEnter"></input>
Bindings
Events: v-on - Modifiers
Event modi ers
.prevent, .stop, .capture, .self, .once
System modi ers
v2.1.0+: .ctrl, .alt, .shift, .meta
v2.5.0+: .exact (single key only)
Mouse button modi ers
.left, .right, .middle
<a v-on:click.prevent="onClick"></a>
Conditionals
v-if, v-else, v-else-if
v-show
always rendered, toggled by CSS classes
v-show vs. v-if
v-if: higher toggle costs
v-show: higher initial render costs
<button v-if="isUserLoggedIn">Logout</button>
<button v-else>Login</button>
<div v-show="showInfo">Hidden info</div>
Lists/loops: v-for
possible for both HTML elements and Vue components
use key attribute for better binding
index or key can be used
<ul>
<li v-for="(user,index) in users" key="user.id">{{ index }} {{ user.name }}</li>
</ul>
<script>
data: {
users: [
{ id: 1, name: 'Bernd' },
{ id: 2, name: 'Stefan' }
]
}
</script>
Slots
like containers inside components
can have default content
name multiple slots
<template>
<div>
<h1>List</h1>
<slot>
Some default content
</slot>
</div>
</template>
<my-component>
<p>Lorem <strong>ipsum</strong> dolor sit amet</p>
</my-component>
<div>
<h1>List</h1>
<p>Lorem <strong>ipsum</strong> dolor sit amet</p>
</div>
Routing
Use component vue-router
<template>
<div id="app">
<router-link to="/demo">Demo</router-link>
<router-view/>
</div>
</template>
<script>
Vue.use(Router)
export default new Router({
routes: [
{
path: '/demo',
name: 'Demo',
component: Demo
}
]
})
</script>
Components
Wrapped in single les with ending .vue
### MyBoxComponent.vue
<template>
<div>
<h1><slot name="title">Default title</slot></h1>
<p><slot>Default content</slot></p>
</div>
</template>
<script>
export default {
data () {
return {
// component data
}
}
}
</script>
<style>
/* some css here */
</style>
Components
<script>
export default {
props: {
// data to pass into component on init
}
data () {
return {
// component data
}
},
methods: {
// component methods go here
},
computed: {
// computed properties with reactive binding
},
watch: {
// watcher functions (rather use computed properties)
}
}
</script>
Components
props
component properties
de ned with type (mandatory) & default value (optional)
props: {
label: {
type: String,
default: 'click me',
required: true
},
importantProp: {
type: Number,
required: true
},
callback: {
type: Function,
default: function() {}
}
}
Components
computed & methods
computed vs methods:
computed properties are cached as long as data does not change
computed: by default only getters, but also setters possible
computed: {
fullname () {
return this.firstname + ' ' + this.lastname; // cached
}
},
methods: {
fullname () {
return this.firstname + ' ' + this.lastname; // always executed
}
}
Components
watch
similar to computed
usually not necessary, rather use computed
Vuex
state management library/pattern
based on/inspired by Flux & Redux
organized in so-called store
store can be split into modules
Vuex
https://vuex.vuejs.org/en/intro.html
Vuex
state
immutable, changed only by mutations
mutations
only place to modify (=mutate) the store state
actions
methods calling mutations
for asynchronous calls
getters
methods to access state properties
use mapGetters in computed of components
add more custom computed properties by using ... (spread operator)
Tipps & tricks
use Chrome plugin Vue
DevTools
great inspections for
components & events
Vuex state management
(with time travel
functionality)
Tipps & tricks
use hot reloading with vue-loader
applies changes without page-reload
enabled by default
make sure to add watchOptions to dev-server.js
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
chunks: false
},
// add these lines if missing
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
})
Links
VueJS guide:
VueJS API:
VueJS video tutorials:
Awesome Vue (link collection):
Vue Devtools (Chrome plugin):
Vuex:
Vuex video tutorials:
https://vuejs.org/v2/guide/
https://vuejs.org/v2/api/
https://laracasts.com/series/learn-vue-2-step-by-
step
https://github.com/vuejs/awesome-vue
https://chrome.google.com/webstore/detail/vuejs-
devtools/nhdogjmejiglipccpnnnanhbledajbpd
https://vuex.vuejs.org/en/
https://www.youtube.com/playlist?
list=PL55RiY5tL51pT0DNJraU93FhMzhXxtDAo
Take aways
Use VueJS!
Split up your app into reusable components
Use Vuex for complex state handling
Stick with simple event (bus) handling for simple apps
Use Vue DevTools for dev/debugging
Thanks for listening
@bazoo0815
https://github.com/coding-berlin/vuejs-demo
https://de.slideshare.net/berndalter7

More Related Content

What's hot (20)

State manager in Vue.js, from zero to Vuex
State manager in Vue.js, from zero to VuexState manager in Vue.js, from zero to Vuex
State manager in Vue.js, from zero to Vuex
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3
 
Vue js for beginner
Vue js for beginner Vue js for beginner
Vue js for beginner
 
Vue, vue router, vuex
Vue, vue router, vuexVue, vue router, vuex
Vue, vue router, vuex
 
Why Vue.js?
Why Vue.js?Why Vue.js?
Why Vue.js?
 
VueJS Introduction
VueJS IntroductionVueJS Introduction
VueJS Introduction
 
Basics of VueJS
Basics of VueJSBasics of VueJS
Basics of VueJS
 
webpack 101 slides
webpack 101 slideswebpack 101 slides
webpack 101 slides
 
Reactjs
ReactjsReactjs
Reactjs
 
Vue.js
Vue.jsVue.js
Vue.js
 
Webpack Introduction
Webpack IntroductionWebpack Introduction
Webpack Introduction
 
Clean backends with NestJs
Clean backends with NestJsClean backends with NestJs
Clean backends with NestJs
 
Vue Vuex 101
Vue Vuex 101Vue Vuex 101
Vue Vuex 101
 
Vue JS Intro
Vue JS IntroVue JS Intro
Vue JS Intro
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Angular
AngularAngular
Angular
 
An introduction to Vue.js
An introduction to Vue.jsAn introduction to Vue.js
An introduction to Vue.js
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 

Similar to Introduction to VueJS & Vuex

Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applicationsAstrails
 
Gutenberg sous le capot, modules réutilisables
Gutenberg sous le capot, modules réutilisablesGutenberg sous le capot, modules réutilisables
Gutenberg sous le capot, modules réutilisablesRiad Benguella
 
Vue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and VuexVue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and VuexChristoffer Noring
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?Tomasz Bak
 
Django + Vue, JavaScript de 3ª generación para modernizar Django
Django + Vue, JavaScript de 3ª generación para modernizar DjangoDjango + Vue, JavaScript de 3ª generación para modernizar Django
Django + Vue, JavaScript de 3ª generación para modernizar DjangoJavier Abadía
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)Igor Bronovskyy
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2Paras Mendiratta
 
Rapid prototyping and easy testing with ember cli mirage
Rapid prototyping and easy testing with ember cli mirageRapid prototyping and easy testing with ember cli mirage
Rapid prototyping and easy testing with ember cli mirageKrzysztof Bialek
 
How to Build SPA with Vue Router 2.0
How to Build SPA with Vue Router 2.0How to Build SPA with Vue Router 2.0
How to Build SPA with Vue Router 2.0Takuya Tejima
 
Aurelia Meetup Paris
Aurelia Meetup ParisAurelia Meetup Paris
Aurelia Meetup ParisAhmed Radjdi
 
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3Rob Tweed
 
2016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES62016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES6양재동 코드랩
 
Angular 1 + es6
Angular 1 + es6Angular 1 + es6
Angular 1 + es6장현 한
 
Introduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon GauvinIntroduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon GauvinSimon Gauvin
 

Similar to Introduction to VueJS & Vuex (20)

An introduction to Vue.js
An introduction to Vue.jsAn introduction to Vue.js
An introduction to Vue.js
 
Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applications
 
Gutenberg sous le capot, modules réutilisables
Gutenberg sous le capot, modules réutilisablesGutenberg sous le capot, modules réutilisables
Gutenberg sous le capot, modules réutilisables
 
Love at first Vue
Love at first VueLove at first Vue
Love at first Vue
 
Vue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and VuexVue fundamentasl with Testing and Vuex
Vue fundamentasl with Testing and Vuex
 
How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?How to replace rails asset pipeline with webpack?
How to replace rails asset pipeline with webpack?
 
Django + Vue, JavaScript de 3ª generación para modernizar Django
Django + Vue, JavaScript de 3ª generación para modernizar DjangoDjango + Vue, JavaScript de 3ª generación para modernizar Django
Django + Vue, JavaScript de 3ª generación para modernizar Django
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
 
Angular JS2 Training Session #2
Angular JS2 Training Session #2Angular JS2 Training Session #2
Angular JS2 Training Session #2
 
Lecture: Webpack 4
Lecture: Webpack 4Lecture: Webpack 4
Lecture: Webpack 4
 
Rapid prototyping and easy testing with ember cli mirage
Rapid prototyping and easy testing with ember cli mirageRapid prototyping and easy testing with ember cli mirage
Rapid prototyping and easy testing with ember cli mirage
 
Backbone js
Backbone jsBackbone js
Backbone js
 
How to Build SPA with Vue Router 2.0
How to Build SPA with Vue Router 2.0How to Build SPA with Vue Router 2.0
How to Build SPA with Vue Router 2.0
 
Aurelia Meetup Paris
Aurelia Meetup ParisAurelia Meetup Paris
Aurelia Meetup Paris
 
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
 
Vue business first
Vue business firstVue business first
Vue business first
 
[W3C HTML5 2016] Angular + ES6
[W3C HTML5 2016] Angular + ES6[W3C HTML5 2016] Angular + ES6
[W3C HTML5 2016] Angular + ES6
 
2016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES62016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES6
 
Angular 1 + es6
Angular 1 + es6Angular 1 + es6
Angular 1 + es6
 
Introduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon GauvinIntroduction to Polymer and Firebase - Simon Gauvin
Introduction to Polymer and Firebase - Simon Gauvin
 

Recently uploaded

Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 

Recently uploaded (20)

Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

Introduction to VueJS & Vuex

  • 2. About me Bernd Alter Technical Director at Turbine Kreuzberg Tech lead, father, boardgame enthusiast, DJ bernd.alter@turbinekreuzberg.com @bazoo0815
  • 4. Basics Use npm package vue-cli Easy scaffolding vue init <template-name> <project-name> vue init webpack my-vue-app
  • 5. Basics Vue instance (single/multiple) Mustache template syntax Lifecycle hooks: created, mounted, updated, destroyed <span>Message: {{ msg }}</span> <script> var vm = new Vue({ data () { return { msg: 'my message' } }, mounted () { this.$emit('mounted'); } // more code }) </script>
  • 6. Bindings Attributes: v-bind (shorthand :) Example: Result: <div class="static" v-bind:class="{ active: isActive, 'danger': hasError }" :text="dynamicText"> </div> <script> data: { isActive: true, hasError: false, dynamicText: 'my dynamic text' } </script> <div class="static active">my dynamic text</div>
  • 7. Bindings Events: v-on (shorthand @) <button v-on:click="onClick">click me</button> <button @click="onClick">click me</button> <script> methods: { onClick () { // do something on clicking the button } } </script>
  • 8. Bindings Events: v-on - Modifiers Key modi ers use key codes or more convenient: built-in modi ers .enter, .tab, .esc, .space, .up, .down, .left, .right, .delete (both 'delete' and 'backspace') <input v-on:keyup.13="onKeyEnter"></input> # same as previous <input @keyup.enter="onKeyEnter"></input>
  • 9. Bindings Events: v-on - Modifiers Event modi ers .prevent, .stop, .capture, .self, .once System modi ers v2.1.0+: .ctrl, .alt, .shift, .meta v2.5.0+: .exact (single key only) Mouse button modi ers .left, .right, .middle <a v-on:click.prevent="onClick"></a>
  • 10. Conditionals v-if, v-else, v-else-if v-show always rendered, toggled by CSS classes v-show vs. v-if v-if: higher toggle costs v-show: higher initial render costs <button v-if="isUserLoggedIn">Logout</button> <button v-else>Login</button> <div v-show="showInfo">Hidden info</div>
  • 11. Lists/loops: v-for possible for both HTML elements and Vue components use key attribute for better binding index or key can be used <ul> <li v-for="(user,index) in users" key="user.id">{{ index }} {{ user.name }}</li> </ul> <script> data: { users: [ { id: 1, name: 'Bernd' }, { id: 2, name: 'Stefan' } ] } </script>
  • 12. Slots like containers inside components can have default content name multiple slots <template> <div> <h1>List</h1> <slot> Some default content </slot> </div> </template> <my-component> <p>Lorem <strong>ipsum</strong> dolor sit amet</p> </my-component> <div> <h1>List</h1> <p>Lorem <strong>ipsum</strong> dolor sit amet</p> </div>
  • 13. Routing Use component vue-router <template> <div id="app"> <router-link to="/demo">Demo</router-link> <router-view/> </div> </template> <script> Vue.use(Router) export default new Router({ routes: [ { path: '/demo', name: 'Demo', component: Demo } ] }) </script>
  • 14. Components Wrapped in single les with ending .vue ### MyBoxComponent.vue <template> <div> <h1><slot name="title">Default title</slot></h1> <p><slot>Default content</slot></p> </div> </template> <script> export default { data () { return { // component data } } } </script> <style> /* some css here */ </style>
  • 15. Components <script> export default { props: { // data to pass into component on init } data () { return { // component data } }, methods: { // component methods go here }, computed: { // computed properties with reactive binding }, watch: { // watcher functions (rather use computed properties) } } </script>
  • 16. Components props component properties de ned with type (mandatory) & default value (optional) props: { label: { type: String, default: 'click me', required: true }, importantProp: { type: Number, required: true }, callback: { type: Function, default: function() {} } }
  • 17. Components computed & methods computed vs methods: computed properties are cached as long as data does not change computed: by default only getters, but also setters possible computed: { fullname () { return this.firstname + ' ' + this.lastname; // cached } }, methods: { fullname () { return this.firstname + ' ' + this.lastname; // always executed } }
  • 18. Components watch similar to computed usually not necessary, rather use computed
  • 19. Vuex state management library/pattern based on/inspired by Flux & Redux organized in so-called store store can be split into modules
  • 21. Vuex state immutable, changed only by mutations mutations only place to modify (=mutate) the store state actions methods calling mutations for asynchronous calls getters methods to access state properties use mapGetters in computed of components add more custom computed properties by using ... (spread operator)
  • 22. Tipps & tricks use Chrome plugin Vue DevTools great inspections for components & events Vuex state management (with time travel functionality)
  • 23. Tipps & tricks use hot reloading with vue-loader applies changes without page-reload enabled by default make sure to add watchOptions to dev-server.js var devMiddleware = require('webpack-dev-middleware')(compiler, { publicPath: webpackConfig.output.publicPath, stats: { colors: true, chunks: false }, // add these lines if missing watchOptions: { aggregateTimeout: 300, poll: 1000 } })
  • 24. Links VueJS guide: VueJS API: VueJS video tutorials: Awesome Vue (link collection): Vue Devtools (Chrome plugin): Vuex: Vuex video tutorials: https://vuejs.org/v2/guide/ https://vuejs.org/v2/api/ https://laracasts.com/series/learn-vue-2-step-by- step https://github.com/vuejs/awesome-vue https://chrome.google.com/webstore/detail/vuejs- devtools/nhdogjmejiglipccpnnnanhbledajbpd https://vuex.vuejs.org/en/ https://www.youtube.com/playlist? list=PL55RiY5tL51pT0DNJraU93FhMzhXxtDAo
  • 25. Take aways Use VueJS! Split up your app into reusable components Use Vuex for complex state handling Stick with simple event (bus) handling for simple apps Use Vue DevTools for dev/debugging