Creating a Vue JS Component with Conditional Rendering in VueJS

Posted by

Vue JS Conditional Rendering

Create a Vue JS Component with Conditional Rendering

In Vue JS, conditional rendering allows you to show or hide elements based on a condition. This can be achieved using the v-if directive in Vue JS. Let’s create a simple Vue JS component with conditional rendering.

This element is displayed

This element is hidden

new Vue({
el: ‘#app’,
data: {
isDisplayed: true
}
});

In the example above, we have created a Vue component with two div elements. The first div is displayed if the isDisplayed data property is true, and the second div is displayed if the isDisplayed data property is false.

We use the v-if and v-else directives to conditionally render these div elements based on the value of the isDisplayed property.

You can toggle the value of the isDisplayed property to show or hide the div elements dynamically. This is a simple example of how conditional rendering can be used in Vue JS components.

In conclusion, conditional rendering is a powerful feature in Vue JS that allows you to show or hide elements based on conditions. By using directives like v-if and v-else, you can easily create dynamic components that respond to user interactions or changing data.