DIRECTIVES

  • Directives are these tiny pieces of HTML attributes that view offers us. They’re always prep-ended with this v and dash. And then it basically attaches some little piece of functionality to that bit of HTML mark up so that you’re not diving into the DOM and doing something with it.

    ../../_images/1.png

CUSTOM DIRECTIVES :-

1. V-FOR :

  • There was the example of li v-for ,we went through previously ,where it loops through the set of values(items in items)
    ../../_images/2.png
  • But we can also do a static number,where we don’t have any data .for eg:’that is given in figure’ –> li v-for num in 5 and then outputting the number, and I can loop through it that way.

    ../../_images/3.png ../../_images/4.png

2. V-MODEL :

  • V-model is super awesome because it establishes a relationship between the data and like a form input.

    ../../_images/5.png
  • For example: here I have some Data(),then I type a message-“THis is a good place to write things.”,then I can output it in booktext ,but I create a relation between the text areas that says v-model,which can dynamically updates values.

    INPUT CODE-

    ../../_images/6.png ../../_images/7.png
  • That means ,whenever I type the message in the above text area it immediately outputted below. we can add animations to it-“animated output”(shown in figure).

    OUTPUT-

    ../../_images/10.png ../../_images/9.png
  • We can even make changes , like I typed above- ‘Hi Frontend Masters’ and erase the previous text ,it will automatically animated in the same way.

    ../../_images/8.png
  • we can also use V-Model for things like check boxes and erase. So if I have something like John, Paul, George, and Ringo. I have all of these check boxes and this is again normal HTML input type-‘checkboxes’ and ID(In image below ).Here v-model is checkedNames .so,if go to vue ,we established the relation with the app and we say that checkboxName is that array. Then whatever name we check ,will be going to show below in checkedNames.

    ../../_images/11.png ../../_images/12.png

3. MODIFIERS :

../../_images/13.png
  • They are of 3 types:-

  • v-model.trim

  • v-model.number

  • v-model.lazy

../../_images/14.png
  • when we use v-model.lazy, it won’t calculate the content automatically ,ie. I am storing the data (under the Type here),until I need it and as the erase button is clicked the output changes are made.

    ../../_images/15.png

4. V-IF/V-SHOW :

  • it’s a conditional that would display information based on the requirements.

    ../../_images/16.png
  • For example- here we have v-model tacos,tacos is a empty -it stars with an empty string .If ,we see button does not shows up until I type any text in the box, let’s say Al Pastor, the button then shows up. If we type v-show in place of v-if ,then it’s going to show the same result, ie. the button will appear only when any text is typed.BUT THERE IS DIFFERENCE BETWEEN BOTH WHEN WE GO AND SEE IN DOM.

    ../../_images/17.png ../../_images/18.png ../../_images/19.png
  • In case of v-if ,there no button present under element until we type something in the box.vf will literally unmount and remount that element into the dom.No button is present under element till we type anything. It will not exist during that time.

    ../../_images/20.png ../../_images/21.png
  • But in case of v-show the button is still present under elements even if we haven’t type anything in the box. V show is just toggling its visibility for us*

    ../../_images/22.png ../../_images/23.png BUTTON PRESENT UNDER 'ELEMENTS'

5. V-IF/V-ELSE :

  • Conditionally render one thing or another thing based on a condition.

    ../../_images/25.png
  • For example: Here we have “ Do you like tacos “ which shows a ‘ Thumbs up ‘ on yes condition and ‘ You are a monster “ on no condition.

    ../../_images/26.png ../../_images/27.png ../../_images/28.png
  • If we take a closer look to understand what’s happening here ,we can see first that tacos is an empty string (as shown in the image below), as it always starts with an empty string and the message shows on screen based on condition. Like here v-if=”tacos===yes”, then thumbs up render ,else condition ~ v-else “ You are a monster “ render.

    ../../_images/29.png ../../_images/30.png

6. V-BIND OR ‘:’

  • We can use “:” instead of writing v-bind in code as it used frequently.

    ../../_images/31.png
  • Using v-bind ,the button would be present in the result screen but in active or inactive form. Like here the button active in color orange when we type something in text box , when nothing is written there it is still present there but in the inactive form.

    ../../_images/32.png

    “Inactive button”

    ../../_images/33.png

    “Active button”

  • Here we can see that the tacos is an empty string and we established a relation that when we type for eg: favourite type of tacos then the button which is present in inactive form would get active on screen.

Note

  • The background color of button can be changed in CSS window. Here we chosen color orange as shown in image below.

../../_images/34.png

7. V-ONCE AND V-PRE:-

../../_images/35.png
  • And v-pre just print the data written as it is on the screen ,it do not process data and if any code is written there it will also starting showing on the screen.like if we change {{tacos}} to {{$data}} ,it will output {{$data}}. v-pre is very useful for documentation.

    ../../_images/36.png ../../_images/38.png

8.V-ON OR @ :

  • We can use “@” instead of writing v-bind in code as it used frequently.

    ../../_images/39.png
  • in js bable ,it is shown that counter =0,initially item =zero. @click=counter>0 means the items ‘+’ will always be greater than zero ; counter+=1 and -=1 means items will increase and decrease by one.

    ../../_images/42.png

9. MULTIPLE BINDINGS:

../../_images/43.png

10. MULTIPLE BINDINGS:

../../_images/44.png

11. V-HTML :

../../_images/45.png

Warning

../../_images/47.png

12. V-TEXT :

../../_images/48.png

Warning

../../_images/51.png

# DIRECTIVES - ALL IN ONE :

../../_images/52.png ../../_images/53.png