Upload
murat-dogan
View
136
Download
1
Embed Size (px)
Citation preview
VuejsGetting Started
Murat Dogan ndash Sr Front End Developer Hurriyet
HelloIrsquom Murat Doğan
bull Senior Front End Developer at Huumlrriyetbull Formerly
bull 1v1ycom bull Doğan Gazetecilik (Milliyet Fanatik Posta Arabamcom) bull Nexum Creative
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
bull What is Vuejs
bull Comparison with other librariesframeworks
bull SFC Concept
bull Getting Started with Vuejs
bull Basic Vuejs Features
bull Creating a development environment in 5 min
bull Okur Temsilcisi Vuejs Edition Vue Router
What are our topics
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
What is Vuejs
bull Vue (pronounced vjuː like view) is a progressive framework for building user
interfaces1
bull It was first released in February 2014 by ex-Google-employee Evan You
bull Only thing you need to know is HTML CSS and JavaScript
bull Itrsquos been popular since its last version 20
bull Vue is used by Alibaba Baidu Expedia Nintendo GitLab mdash a list of smaller projects can be
found on madewithvuejscom
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How to choose the right framework
bull How mature are the frameworks librariesbull How extensive and helpful are their corresponding communitiesbull How easy is it to find developers for each of the frameworksbull What are the basic programming concepts of the frameworksbull How easy is it to use the frameworks for small or large applicationsbull What does the learning curve look like for each frameworkbull What kind of performance can you expect from the frameworksbull Where can you have a closer look under the hoodbull How can you start developing with the chosen framework
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Comparison with other librariesframeworks
Based on 3rd party benchmark lower is better
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
HelloIrsquom Murat Doğan
bull Senior Front End Developer at Huumlrriyetbull Formerly
bull 1v1ycom bull Doğan Gazetecilik (Milliyet Fanatik Posta Arabamcom) bull Nexum Creative
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
bull What is Vuejs
bull Comparison with other librariesframeworks
bull SFC Concept
bull Getting Started with Vuejs
bull Basic Vuejs Features
bull Creating a development environment in 5 min
bull Okur Temsilcisi Vuejs Edition Vue Router
What are our topics
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
What is Vuejs
bull Vue (pronounced vjuː like view) is a progressive framework for building user
interfaces1
bull It was first released in February 2014 by ex-Google-employee Evan You
bull Only thing you need to know is HTML CSS and JavaScript
bull Itrsquos been popular since its last version 20
bull Vue is used by Alibaba Baidu Expedia Nintendo GitLab mdash a list of smaller projects can be
found on madewithvuejscom
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How to choose the right framework
bull How mature are the frameworks librariesbull How extensive and helpful are their corresponding communitiesbull How easy is it to find developers for each of the frameworksbull What are the basic programming concepts of the frameworksbull How easy is it to use the frameworks for small or large applicationsbull What does the learning curve look like for each frameworkbull What kind of performance can you expect from the frameworksbull Where can you have a closer look under the hoodbull How can you start developing with the chosen framework
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Comparison with other librariesframeworks
Based on 3rd party benchmark lower is better
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
bull What is Vuejs
bull Comparison with other librariesframeworks
bull SFC Concept
bull Getting Started with Vuejs
bull Basic Vuejs Features
bull Creating a development environment in 5 min
bull Okur Temsilcisi Vuejs Edition Vue Router
What are our topics
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
What is Vuejs
bull Vue (pronounced vjuː like view) is a progressive framework for building user
interfaces1
bull It was first released in February 2014 by ex-Google-employee Evan You
bull Only thing you need to know is HTML CSS and JavaScript
bull Itrsquos been popular since its last version 20
bull Vue is used by Alibaba Baidu Expedia Nintendo GitLab mdash a list of smaller projects can be
found on madewithvuejscom
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How to choose the right framework
bull How mature are the frameworks librariesbull How extensive and helpful are their corresponding communitiesbull How easy is it to find developers for each of the frameworksbull What are the basic programming concepts of the frameworksbull How easy is it to use the frameworks for small or large applicationsbull What does the learning curve look like for each frameworkbull What kind of performance can you expect from the frameworksbull Where can you have a closer look under the hoodbull How can you start developing with the chosen framework
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Comparison with other librariesframeworks
Based on 3rd party benchmark lower is better
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
What is Vuejs
bull Vue (pronounced vjuː like view) is a progressive framework for building user
interfaces1
bull It was first released in February 2014 by ex-Google-employee Evan You
bull Only thing you need to know is HTML CSS and JavaScript
bull Itrsquos been popular since its last version 20
bull Vue is used by Alibaba Baidu Expedia Nintendo GitLab mdash a list of smaller projects can be
found on madewithvuejscom
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How to choose the right framework
bull How mature are the frameworks librariesbull How extensive and helpful are their corresponding communitiesbull How easy is it to find developers for each of the frameworksbull What are the basic programming concepts of the frameworksbull How easy is it to use the frameworks for small or large applicationsbull What does the learning curve look like for each frameworkbull What kind of performance can you expect from the frameworksbull Where can you have a closer look under the hoodbull How can you start developing with the chosen framework
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Comparison with other librariesframeworks
Based on 3rd party benchmark lower is better
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How to choose the right framework
bull How mature are the frameworks librariesbull How extensive and helpful are their corresponding communitiesbull How easy is it to find developers for each of the frameworksbull What are the basic programming concepts of the frameworksbull How easy is it to use the frameworks for small or large applicationsbull What does the learning curve look like for each frameworkbull What kind of performance can you expect from the frameworksbull Where can you have a closer look under the hoodbull How can you start developing with the chosen framework
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Comparison with other librariesframeworks
Based on 3rd party benchmark lower is better
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How to choose the right framework
bull How mature are the frameworks librariesbull How extensive and helpful are their corresponding communitiesbull How easy is it to find developers for each of the frameworksbull What are the basic programming concepts of the frameworksbull How easy is it to use the frameworks for small or large applicationsbull What does the learning curve look like for each frameworkbull What kind of performance can you expect from the frameworksbull Where can you have a closer look under the hoodbull How can you start developing with the chosen framework
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Comparison with other librariesframeworks
Based on 3rd party benchmark lower is better
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Comparison with other librariesframeworks
Based on 3rd party benchmark lower is better
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Stars of librariesframeworks ndash 11th of September
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
State of librariesframeworks
httpsstateofjscom2017front-endresults
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Installing Vuejs
bull You can install core library via CDN (Content Delivery Network)bull If you want to always follow up the latest version of Vue use unpkg
bull httpsunpkgcomvue - Always redirect to the latest version of Vuebull httpsunpkgcomvue[version]distvueminjs - to specific version
bull You can install core library using Bower or npm
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Getting Started ndash Hello World
bull Open httpsjsfiddlenet bull Choose the additional JS Framework Vuejsbull JavaScript section
bull new Vue(elapp)bull HTML section
bull ltdiv id=appgt Hello + world ltdivgtbull Result
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How it works
bull new Vue(elapp) will instantiate a new Vue instance bull It accepts an options object as a parameter bull This object is central in Vue and defines and controls data and behaviorbull It contains all the information needed to create Vue instances and components bull In our case we only specified the el option which accepts a selector or an element as an argument bull The app parameter is a selector that will return the element in the page with app as the identifier
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
How JsFiddle works
Everything that we will write inside the ltdivgt with the ID as app will be under the scope of VueNow JSFiddle takes everything we write in the HTML quadrant and wraps it in body tags This means that if we just need to write the ltdivgt in the HTML quadrant JSFiddle will take care of wrapping it in the body tags
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Its also important to note that placing the app on the body or html tag will throw an error as Vue advises us to mount our apps on normal elements and its the same thing goes for selecting the body in the el option
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull Each Vue instance goes through a series of initialization steps when itrsquos created
bull it also runs functions called lifecycle hooks giving users the opportunity to add their own code at specific stages
bull For example the created hook can be used to run code after an instance is created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks
bull There are also other hooks which will be called at different stages of the instancersquos lifecycle such
as mounted updated and destroyed
bull All lifecycle hooks are called with their this context pointing to the Vue instance invoking it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Instance Lifecycle Hooks ndash Warning
bull Donrsquot use arrow functions on an options property or callback such as
bull created () =gt consolelog(thisa) or vm$watch(a newValue =gt thismyMethod())
bull Since arrow functions are bound to the parent context this will not be the Vue instance as yoursquod expect often
resulting in errors such as
Uncaught TypeError Cannot read property of undefined or Uncaught TypeError thismyMethod is not a function
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram
Creation Mounting Updating Destruction
beforeCreate
Created
beforeMount
Mounted
beforeUpdate
Updated
beforeDestroy
Destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Creation
bull Creation hooks are the very first hooks that run in your component
bull They allow you to perform actions before your component has even been added to the DOM
bull Unlike any of the other hooks creation hooks are also run during server-side rendering
bull Use creation hooks if you need to set things up in your component both during client rendering and server
rendering
bull You will not have access to the DOM or the target mounting element (this$el) inside of creation hooks
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt beforeCreate
bull The beforeCreate hook runs at the very initialization of your component data has not been made reactive
and events have not been set up yet
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
bull In the created hook you will be able to access reactive data and events are active Templates and Virtual
DOM have not yet been mounted or rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Creation gt created
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Mounting
bull Mounting hooks are often the most-used hooks for better or worse
bull They allow you to access your component immediately before and after the first render
bull They do not however run during server-side rendering
bull Use if You need to access or modify the DOM of your component immediately before or after the initial
render
bull Do not use if You need to fetch some data for your component on initialization
Use created (or created + activated for keep-alive components) for this instead especially if you need that
data during server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
bull The beforeMount hook runs right before the initial render happens and after the template or render
functions have been compiled Most likely yoursquoll never need to use this hook
bull Remember it doesnrsquot get called when doing server-side rendering
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt beforeMount
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
bull In the mounted hook you will have full access to the reactive component templates and rendered DOM
(via this$el) Mounted is the most-often used lifecycle hook
bull The most frequently used patterns are fetching data for your component (use created for this instead) and
modifying the DOM often to integrate non-Vue libraries
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Mounting gt mounted
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Updating
bull Updating hooks are called whenever a reactive property used by your component changes or something
else causes it to re-render
bull They allow you to hook into the watch-compute-render cycle for your component
bull Use if You need to know when your component re-renders perhaps for debugging or profiling
bull Do not use if You need to know when a reactive property on your component changes Use computed
properties or watchers for that instead
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
bull The beforeUpdate hook runs after data changes on your component and the update cycle begins right
before the DOM is patched and re-rendered
bull It allows you to get the new state of any reactive data on your component before it actually gets rendered
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt beforeUpdate
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
The updated hook runs after data changes on your component and the DOM re-renders If you need to access
the DOM after a property change here is probably the safest place to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Updating gt updated
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram - Destruction
bull Destruction hooks allow you to perform actions when your component is destroyed such as cleanup or
analytics sending They fire when your component is being torn down and removed from the DOM
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt beforeDestroy
bull beforeDestroy is fired right before teardown Your component will still be fully present and functional If you
need to cleanup events or reactive subscriptions beforeDestroy would probably be the time to do it
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Destruction gt destroyed
bull By the time you reach the destroyed hook therersquos pretty much nothing left on your component Everything
that was attached to it has been destroyed You might use the destroyedhook to do any last-minute cleanup
or inform a remote server that the component was destroyed
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Lifecycle Diagram gt Other Hooks
bull There are two other hooks activated and deactivated These are for keep-alive components a topic that is
outside the scope of this presentation
bull Suffice it to say that they allow you to detect when a component that is wrapped in a ltkeep-alivegtltkeep-
alivegt tag is toggled on or off You might use them to fetch data for your component or handle state changes
effectively behaving as created and beforeDestroy without the need to do a full component rebuild
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Template Syntax - Interpolations
Text Raw Html
this will also affect any binding on the same node
Attributes
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Interpolations - JavaScript Expressions
So far wersquove only been binding to simple property keys in our templates But Vuejs actually supports the full power of JavaScript expressions inside all data bindings
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Directives
Directives are special attributes with the v- prefix Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for which will be discussed later) A directiversquos job is to reactively apply side effects to the DOM when the value of its expression changes Letrsquos review the example we saw in the introduction
v-if v-show
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-if vs v-show
bull v-if is ldquorealrdquo conditional rendering because it ensures that event listeners and child components inside the
conditional block are properly destroyed and re-created during toggles
bull v-if is also lazy if the condition is false on initial render it will not do anything - the conditional block wonrsquot be
rendered until the condition becomes true for the first time
bull In comparison v-show is much simpler - the element is always rendered regardless of initial condition with just
simple CSS-based toggling
bull Generally speaking v-if has higher toggle costs while v-show has higher initial render costs So prefer v-show if you
need to toggle something very often and prefer v-if if the condition is unlikely to change at runtime
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Arguments
Some directives can take an ldquoargumentrdquo denoted by a colon after the directive name For example the v-bind directive is used to reactively update an HTML attribute
Here href is the argument which tells the v-bind directive to bind the elementrsquos href attribute to the value of the expression url
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifiers
Modifiers are special postfixes denoted by a dot which indicate that a directive should be bound in some special way For example the prevent modifier tells the v-on directive to call eventpreventDefault() on the triggered event
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Shorthands
The v- prefix serves as a visual cue for identifying Vue-specific attributes in your templates This is useful when you are using Vuejs to apply dynamic behavior to some existing markup but can feel verbose for some frequently used directives At the same time the need for the v- prefix becomes less important when you are building an SPA where Vuejs manages every template Therefore Vuejs provides special shorthands for two of the most often used directives v-bind and v-on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
In-template expressions are very convenient but they are really only meant for simple operations Putting too much logic into your templates can make them bloated and hard to maintain For example
At this point the template is no longer simple and declarative Thatrsquos why for any complex logic you should use a computed property
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed properties
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Computed caching vs Methods
You may have noticed we can achieve the same result by invoking a method in the expression
However the difference is that computed properties are cached based on their dependencies A computed property will only re-evaluate when some of its dependencies have changed This means as long as messagehas not changed multiple access to the reversedMessage computed property will immediately return the previously computed result without having to run the function again
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding HTML Classes
Object Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Array Syntax
The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
The object syntax for v-bindstyle is pretty straightforward - it looks almost like CSS except itrsquos a JavaScript object You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Object Syntax
It is often a good idea to bind to a style object directly so that the template is cleaner
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Binding Inline Styles
Auto-prefixing
When you use a CSS property that requires vendor prefixes in v-bindstyle for example transform Vue will automatically detect and add appropriate prefixes to the applied styles
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
We can use the v-for directive to render a list of items based on an array The v-fordirective requires a special syntax in the form of item in items where items is the source data array and item is an alias for the array element being iterated on
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
List Rendering
Inside v-for blocks we have full access to parent scope properties v-for also supports an optional second argument for the index of the current item
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with an Object
You can also use v-for to iterate through the properties of an object
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with a Range
v-for can also take an integer In this case it will repeat the template that many times
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
v-for with v-if
When they exist on the same node v-for has a higher priority than v-if That means the v-if will be run on each iteration of the loop separately This can be useful when you want to render nodes for only some items like below
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Handling
We can use the v-on directive to listen to DOM events and run some JavaScript when theyrsquore triggeredFor example
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Method Event Handlers
The logic for many event handlers will be more complex though so keeping your JavaScript in the value of the v-on attribute simply isnrsquot feasible Thatrsquos why v-on can also accept the name of a method yoursquod like to call
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Methods in Inline Handlers
Instead of binding directly to a method name we can also use methods in an inline JavaScript statement
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
It is a very common need to call eventpreventDefault() or eventstopPropagation()inside event handlers
To address this problem Vue provides event modifiers for v-on Recall that modifiers are directive postfixes denoted
by a dot
bull stop
bull prevent
bull capture
bull self
bull once
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Event Modifiers
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
When listening for keyboard events we often need to check for common key codes Vue also allows adding key modifiers for v-on when listening for key events
Remembering all the keyCodes is a hassle so Vueprovides aliases for the most commonly used keys
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
Herersquos the full list of key modifier aliases
bull enter
bull tab
bull delete (captures both ldquoDeleterdquo and ldquoBackspacerdquo keys)
bull esc
bull space
bull up
bull down
bull left
bull right
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
You can also define custom key modifier aliases via the global configkeyCodes object
ltinput v-onkeyupf1=submitgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Key Modifiers
VueconfigkeyCodes = v 86f1 112 camelCase won`t workmediaPlayPause 179 instead you can use kebab-case with double
quotation marksmedia-play-pause 179up [38 87]
ltinput type=text keyupmedia-play-pause=methodgt
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Modifier Keys
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressedbull ctrlbull altbull shiftbull meta
Note On Macintosh keyboards meta is the command key () On Windows keyboards meta is the windows key (⊞)
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Mouse Button Modifiers
New in 220+bull leftbull rightbull middleThese modifiers restrict the handler to events triggered by a specific mouse button
httpsjsfiddlenetz1jhpewo1
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
SFC Concept (Single File Components)
What About Separation of ConcernsOne important thing to note is that separation of concerns is not equal to separation of file types
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Development Environment in 5 min
vue-webpack-boilerplate is the best practice to start from scratch
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Thank YouYou can find me
Github muratdogan17
Linkedin muratdogan17
Twitter murat_dogan17
Medium muratdogan
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle
Murat Dogan ndash Sr Front End Developer HurriyetVuejs ndash Getting Started
Creditsbull httpsvuejsorgv2guide
bull httpsvuejsorgv2guidesingle-file-componentshtml
bull httpswwwpacktpubcommaptbookweb_development9781786468093
bull httpsapppluralsightcomlibrarycoursesvuejs-getting-startedtable-of-contents
bull httpsmediumcomthe-vue-pointvue-2-0-is-here-ef1f26acf4b8
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsmediumcomunicorn-suppliesangular-vs-react-vs-vue-a-2017-comparison-c5c52d620176
bull httpsalligatoriovuejscomponent-lifecycle