Vue Directives

v-bind, v-if, v-show and v-for.

✍️ 3 exercises + code challenge in this lesson

Directives are special attributes that start with v-:

  • v-bind:src="url" or the shorthand :src="url": bind an attribute.
  • v-if / v-else-if / v-else: add or remove elements.
  • v-show: toggle visibility with CSS.
  • v-for="item in items": repeat an element; give each a :key.

Key Takeaways: Vue Directives

Mastering Vue Directives helps you write clean and reliable Vue.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Vue Directives Code Example

Vue.js
<script src="https://unpkg.com/vue@3.5.12/dist/vue.global.prod.js"></script>

<div id="app">
  <img :src="logo" alt="Vue logo" width="60">
  <p v-if="score >= 50">Passed with {{ score }}</p>
  <p v-else>Failed</p>
  <ul>
    <li v-for="lang in langs" :key="lang">{{ lang }}</li>
  </ul>
</div>

<script>
  Vue.createApp({
    data() {
      return { logo: "https://vuejs.org/images/logo.png", score: 72, langs: ["HTML", "CSS", "Vue"] };
    }
  }).mount("#app");
</script>
Free sign-in required · Google login available

Vue Directives Practice Exercises

Check what you learned in “Vue Directives”.

/ 3 solved
Fill in the blank

Repeat the <li> for every item.

<li ="item in items" :key="item">{{ item }}</li>
✓ Correct! Not quite. Try again.
Multiple choice

What is the shorthand for v-bind:href?

✓ Correct! Not quite. Try again.
Code challenge

Render the colors array as <li> items with v-for.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services