Render Content Conditionally with v-if in Vue

Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated a year ago

We'll see how we can use the v-if directive in Vue to render content based on a conditional statement. We'll also see the v-else and the v-else-if directives in action. The v-else and v-else-if blocks need to immediately follow the v-if block to function properly.

Instructor: [0:00] We've seen the directive of Vue bind, which we can abbreviate to a colon, but Vue has other directives. The next one we're going to look at is v-if, Vue-if, and this conditionally renders the block based on the evaluation of whatever is in these quotes.

[0:17] If I was to type in false here, that would be evaluated in JavaScript and that block is no longer there. True, equally would be evaluated. If I was to base it on my tickets, say if tickets are greater than zero, then show that block. Then I can decrement the tickets. One ticket zero, and they're gone but if there are tickets, then we have that block being rendered.

[0:43] We can have a v-else block. It has to come directly after the VF block for it to be recognized. Here, I have 10 tickets as I decrement down. This block is not being rendered anymore, so we can remove our ternary operation that we had before. That block's not being rendered anymore.

[1:07] Instead, this block is being rendered. There are no tickets. What if we wanted to warn people that tickets are close to selling out? How about we use the v-else-if directive. Here, we may have our normal tickets and we might not tell people the actual numbers. We might say, on sale now.

[1:29] If the tickets are, let's say, less than or equal to 15 and greater than , we could say, tickets are running low. Otherwise, we could say, sorry, we're sold out. At 10 tickets, tickets are running low. At 20 tickets, it's on sale now. At zero tickets, sorry, we're sold out.

[1:54] As long as these blocks immediately succeed each other, this logic will be carried out. The VF will be evaluated first, and if that's true, then that will be rendered. The v-else-if will be rendered next, otherwise the VLs.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today