SlideShare a Scribd company logo
1 of 6
Download to read offline
Learning Vue Directives
This is taken from Sara Drasner's course on Frontend Masters. I did the original one
when I was just learning Vue for the first time. Since then I have worked with Vue
for a good few years and there are still things I am learning. These are my notes
from the first few lessons. Hope it helps someone else learn something they didn't
already know.
v-text
v-text works by setting the element's textContent property, so it will overwrite any existing
content inside the element. If you need to update the part of textContent, you should use
mustache interpolations instead.
<span v-text="msg"></span>
<!-- same as -->
<span>{{msg}}</span>
v-html
Contents of v-html are inserted as plain HTML - Vue template syntax will not be processed.
If you find yourself trying to compose templates using v-html, try to rethink the solution by
using components instead.
<div v-html="html"></div>
v-show
Toggle the element's visibility based on the truthy-ness of the expression value.
v-show works by setting the display CSS property via inline styles, and will try to respect
the initial display value when the element is visible. It also triggers transitions when its
condition changes.
v-if
Conditionally render an element or a template fragment based on the truthy-ness of the
expression value.
When a v-if element is toggled, the element and its contained directives / components are
destroyed and re-constructed. If the initial condition is falsy, then the inner content won't be
rendered at all.
Can be used on <template> to denote a conditional block containing only text or multiple
elements.
This directive triggers transitions when its condition changes.
When used together, v-if has a higher priority than v-for. We don't recommend using these
two directives together on one element — see the list rendering guide for details.
v-else
Denote the "else block" for v-if or a v-if / v-else-if chain.
○ Restriction: previous sibling element must have v-if or v-else-if.
○ Can be used on <template> to denote a conditional block containing only
text or multiple elements.
Example
<div v-if="Math.random() > 0.5">
Now you see me
</div>
<div v-else>
Now you don't
</div>
v-else-if
Denote the "else if block" for v-if. Can be chained.
○ Restriction: previous sibling element must have v-if or v-else-if.
○ Can be used on <template> to denote a conditional block containing only
text or multiple elements.
● Example
<div v-if="type === 'A'">
A
</div>
<div v-else-if="type === 'B'">
B
</div>
<div v-else-if="type === 'C'">
C
</div>
<div v-else>
Not A/B/C
</div>
Lazy Models
v-model.lazy for when you don't want it to be always watching for changes and
only modify the DOM when you click something for example when you type and
then click and it appears on the page only when you click and not as you type.
<input v-model.lazy="msg" />
Trimming your Models
v-modal-trim will automatically trim whitespace in your inputs.
<input v-model.trim="msg" />
Making your Models a Number
v-model-number will convert any string to a number. We can use type="number"
but the value of HTML input elements will always return a string and if it can't be
parsed with parseFloat() the original value is returned. Therefore using
v-model-number ensures our input is of type Number.
<input v-model.number="age" type="number" />
Only show your Element Once
v-once is used to show things only once. If you modify the input text you will not
see the results change. The v-once will remain to show the same text. When the
page re-renders, the element/component and all its children will be treated as static
content and skipped.
<span v-once>This will never be updated: {{msg}}</span>
V-pre for Documenting
V-pre will not evaluate any text that adds moustache syntax, for example, and will
show it exactly as it is written.
<span v-pre>{{ this will not be compiled }}</span>
tag with $data
<pre> tag can be used with {{$data}} inside it and it will show you all the data you
have in your data property of that file/component.
Click and Mouse Events
v-on is the same as @ symbol and is great for binding events like click and
mouseenter.
<button v-on:click="counter += 1">Add 1</button>
You can use a ternary operator inside a click event which is good for showing
making sure a counter doesn't go below 0, for example.
<button @click="counter > 0 ? counter -=1 : 0">Add 1</button>
You can do multiple bindings which are ideal for games such as mouseup and
mousedown doing different things.
@mousemove.stop is comparable to e.stopPropagation().
@mousemove.prevent is like e.preventDefault().
@submit.prevent will stop the page from refreshing when submitting.
@click.once this click event will be triggered once not to be confused with v-once.
The click event will only be triggered once as opposed to v-once where the text
will be only rendered once. Good for when you want to stop something from
submitting multiple times or something to be clicked and then you don't want the
user to be able to keep on clicking, they can but it doesn't keep submitting or doing
what it has probably already done
@click.native so you can listen to native events in the DOM.
Key Codes
You can use key codes but this is changing in vue 3 to only use names same as
HTML spec.
Rendering HTML
v-html is not recommended if coming from an external source.
<div v-html="html"></div>
Rendering Text
v-text is the same as using the moustache templates, {{}}, and if you want to
dynamically update then it is recommended to use moustache templates and not
v-text
Read more on programming tutorials and download cheat sheets

More Related Content

Similar to Learning Vue Directives.pdf

Paca java script slid
Paca java script slidPaca java script slid
Paca java script slid
pacatarpit
 
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and JasmineRails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basics
robertbenard
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basics
robertbenard
 
Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]
srikanthbkm
 
wp-UNIT_III.pptx
wp-UNIT_III.pptxwp-UNIT_III.pptx
wp-UNIT_III.pptx
GANDHAMKUMAR2
 
Future proofing design work with Web components
Future proofing design work with Web componentsFuture proofing design work with Web components
Future proofing design work with Web components
btopro
 

Similar to Learning Vue Directives.pdf (20)

Vue.js Getting Started
Vue.js Getting StartedVue.js Getting Started
Vue.js Getting Started
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
13 javascript techniques to improve your code
13 javascript techniques to improve your code13 javascript techniques to improve your code
13 javascript techniques to improve your code
 
Introduction to Vue.js
Introduction to Vue.jsIntroduction to Vue.js
Introduction to Vue.js
 
Paca java script slid
Paca java script slidPaca java script slid
Paca java script slid
 
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and JasmineRails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basics
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basics
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Angular js animate shashikant bhongale -20-7-16
Angular js animate shashikant bhongale -20-7-16Angular js animate shashikant bhongale -20-7-16
Angular js animate shashikant bhongale -20-7-16
 
Unit 2.4
Unit 2.4Unit 2.4
Unit 2.4
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
wp-UNIT_III.pptx
wp-UNIT_III.pptxwp-UNIT_III.pptx
wp-UNIT_III.pptx
 
Knockoutjs
KnockoutjsKnockoutjs
Knockoutjs
 
RSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodRSpec 3: The new, the old, the good
RSpec 3: The new, the old, the good
 
Eloquent ruby
Eloquent rubyEloquent ruby
Eloquent ruby
 
Future proofing design work with Web components
Future proofing design work with Web componentsFuture proofing design work with Web components
Future proofing design work with Web components
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learning
 

Recently uploaded

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 

Recently uploaded (20)

PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 

Learning Vue Directives.pdf

  • 1. Learning Vue Directives This is taken from Sara Drasner's course on Frontend Masters. I did the original one when I was just learning Vue for the first time. Since then I have worked with Vue for a good few years and there are still things I am learning. These are my notes from the first few lessons. Hope it helps someone else learn something they didn't already know. v-text v-text works by setting the element's textContent property, so it will overwrite any existing content inside the element. If you need to update the part of textContent, you should use mustache interpolations instead. <span v-text="msg"></span> <!-- same as --> <span>{{msg}}</span> v-html Contents of v-html are inserted as plain HTML - Vue template syntax will not be processed. If you find yourself trying to compose templates using v-html, try to rethink the solution by using components instead. <div v-html="html"></div> v-show Toggle the element's visibility based on the truthy-ness of the expression value. v-show works by setting the display CSS property via inline styles, and will try to respect the initial display value when the element is visible. It also triggers transitions when its condition changes.
  • 2. v-if Conditionally render an element or a template fragment based on the truthy-ness of the expression value. When a v-if element is toggled, the element and its contained directives / components are destroyed and re-constructed. If the initial condition is falsy, then the inner content won't be rendered at all. Can be used on <template> to denote a conditional block containing only text or multiple elements. This directive triggers transitions when its condition changes. When used together, v-if has a higher priority than v-for. We don't recommend using these two directives together on one element — see the list rendering guide for details. v-else Denote the "else block" for v-if or a v-if / v-else-if chain. ○ Restriction: previous sibling element must have v-if or v-else-if. ○ Can be used on <template> to denote a conditional block containing only text or multiple elements. Example <div v-if="Math.random() > 0.5"> Now you see me </div> <div v-else> Now you don't </div> v-else-if Denote the "else if block" for v-if. Can be chained. ○ Restriction: previous sibling element must have v-if or v-else-if.
  • 3. ○ Can be used on <template> to denote a conditional block containing only text or multiple elements. ● Example <div v-if="type === 'A'"> A </div> <div v-else-if="type === 'B'"> B </div> <div v-else-if="type === 'C'"> C </div> <div v-else> Not A/B/C </div> Lazy Models v-model.lazy for when you don't want it to be always watching for changes and only modify the DOM when you click something for example when you type and then click and it appears on the page only when you click and not as you type. <input v-model.lazy="msg" /> Trimming your Models v-modal-trim will automatically trim whitespace in your inputs. <input v-model.trim="msg" /> Making your Models a Number
  • 4. v-model-number will convert any string to a number. We can use type="number" but the value of HTML input elements will always return a string and if it can't be parsed with parseFloat() the original value is returned. Therefore using v-model-number ensures our input is of type Number. <input v-model.number="age" type="number" /> Only show your Element Once v-once is used to show things only once. If you modify the input text you will not see the results change. The v-once will remain to show the same text. When the page re-renders, the element/component and all its children will be treated as static content and skipped. <span v-once>This will never be updated: {{msg}}</span> V-pre for Documenting V-pre will not evaluate any text that adds moustache syntax, for example, and will show it exactly as it is written. <span v-pre>{{ this will not be compiled }}</span> tag with $data
  • 5. <pre> tag can be used with {{$data}} inside it and it will show you all the data you have in your data property of that file/component. Click and Mouse Events v-on is the same as @ symbol and is great for binding events like click and mouseenter. <button v-on:click="counter += 1">Add 1</button> You can use a ternary operator inside a click event which is good for showing making sure a counter doesn't go below 0, for example. <button @click="counter > 0 ? counter -=1 : 0">Add 1</button> You can do multiple bindings which are ideal for games such as mouseup and mousedown doing different things. @mousemove.stop is comparable to e.stopPropagation(). @mousemove.prevent is like e.preventDefault(). @submit.prevent will stop the page from refreshing when submitting. @click.once this click event will be triggered once not to be confused with v-once. The click event will only be triggered once as opposed to v-once where the text will be only rendered once. Good for when you want to stop something from submitting multiple times or something to be clicked and then you don't want the user to be able to keep on clicking, they can but it doesn't keep submitting or doing what it has probably already done @click.native so you can listen to native events in the DOM. Key Codes You can use key codes but this is changing in vue 3 to only use names same as HTML spec. Rendering HTML
  • 6. v-html is not recommended if coming from an external source. <div v-html="html"></div> Rendering Text v-text is the same as using the moustache templates, {{}}, and if you want to dynamically update then it is recommended to use moustache templates and not v-text Read more on programming tutorials and download cheat sheets