Directive v-if

Introduction

These directives are embedded as attributes in the desired HTML elements and can replace the familiar javascript syntax:

vue.js

Syntax and examples

v-if

It is used for conditional rendering of the element ie. can add and remove an element from the DOM.

NOTE:
If the “v-for” directive is used together with “v-if” on the same element, the “v-for” directive takes precedence over “v-if.

v-else-if

The element to which the "v-else-if" directive is attached must be right after the HTML element to which the "v-if" directive is attached, otherwise Vue.js will not recognize it.

v-else

The element to which the "v-else" directive is attached must be immediately after the HTML elements on which they are attached "v-if" or "v-else-if" directives, if this is not respected Vue.js will not recognize it.

Example

Since the v-else directive must be on the element that is right after the element that has the “if” directive attached to it, if we need to affect more elements, we have to wrap the desired elements with a “wrapper” element. That wrapper can be a “div” tag, but it is recommended to use <template> tag, because it “does not pollute” the DOM (the tag is not rendered, but its content is).