Conditional Directives with Vue.js - Laravel front-end development


The ability to show or hide elements based on conditions is a fundamental feature of Laravel frontend framework Vue.js. It shipped with a set of core directives to achieve this: v-if, v-else, v-else-if and v-show.

when we get started with a Vue.js Hello World application, we can use values from Vue.js data property object to conditionally control the view. For example, we’ll see how to use the simple data object given below.

 export default{
        data() {
            return {
                message: "Hello World!",
                isSignedIn: false
            }
        }
 }

v-if directive 


The v-if directive adds or removes front end DOM elements based on the given expression.
We can use the isSignedIn property from the vue data object and show a signin button in the vue js component or Laravel view as in the example bellow.

<button v-if="isSignedIn">Logout</button>

Now, the button will not show because isSignedIn is set to false. Setting the data.isSignedIn value to true will add the button to the DOM.

<root> element

The v-if directive can only show or hide one element (and its child elements), but you can also control multiple elements with a single v-if reducing duplication.

To do this, you need to wrap all the elements that should be controlled by this condition in a root element such as div . All elements under the root element will be added or removed depending on the v-if expression value.

For example, if you need to show a label as well as a button when the isSignedIn is true you can wrap both elements in a single div element as follows.

<div v-if="isSignedIn">
  <label> Logout </button>
  <button> Logout </button>
</div>

v-else directive 


As the name v-else suggests, this directive is used to display content only when the expression adjacent v-if resolves to false.

We can have a Log In button to show automatically when the isSignedIn is false.

<button v-if="isSignedIn"> Logout </button>
<button v-else> Log In </button>

v-else does not need an expression defined in to it. But it must be in an element that comes immediately after an element containing v-if or v-else-if directives.

v-else-if directive 


v-else-if can be used when we need more than two options to be checked. This will ensure that only one of the chained items in the else-if chain will be visible.

For example, if the property named isSignedInDisabled is true, we can prevent the Log In button from displaying and instead display a label. We can accomplish it by using the v-else-if directive as follows.

<button v-if="isSignedIn"> Logout </button>
<label v-else-if="isSignedInDisabled"> Register disabled </label>
<button v-else> Log In </button>


v-show directive


Very similar to v-if, the v-show directive can also be used to show and hide an element based on an expression.

The main difference between the two is that,

    v-if - Only renders the element to the DOM if the expression passes.
    v-show - Renders all elements to the DOM and then uses the CSS display property to hide elements if the expression fails.
    v-show - Does not support v-else, v-else-if

Usually, v-show has a performance advantage if the elements are switched on and off frequently, while the v-if has the advantage when it comes to initial render time.

Written by Akram Wahid 6 years ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}