Special Directives

Loading concept...

Vue.js Special Directives: The Secret Shortcuts! 🎯

The Magic Wand Story

Imagine you have a magic wand that writes on a special chalkboard. Normally, this wand is very smart — it watches what you write and changes the board whenever you tell it something new.

But sometimes… you don’t want the wand to be so smart!

  • v-pre: “Dear wand, don’t read this part. Just show it exactly as I wrote it!”
  • v-once: “Dear wand, write this ONE time and never change it again!”
  • v-for with v-if: “Wait! You’re trying to do TWO things at once — that’s confusing!”

Let’s explore each one!


đź”’ The v-pre Directive

What is v-pre?

Think of v-pre as putting tape over the magic wand’s eyes. Vue normally looks at {{ }} and tries to replace it with real data. But with v-pre, Vue says:

“I won’t touch this. I’ll show it exactly as written!”

Why Use It?

  1. Show Vue code without Vue changing it
  2. Speed up rendering for content that never needs Vue
  3. Documentation — when teaching Vue itself!

Simple Example

<!-- Without v-pre -->
<p>{{ message }}</p>
<!-- Shows: Hello World -->

<!-- With v-pre -->
<p v-pre>{{ message }}</p>
<!-- Shows: {{ message }} literally! -->

Real Life Example

<div v-pre>
  <h3>Vue Syntax Tutorial</h3>
  <p>Use {{ variableName }}
     to display data</p>
  <p>Use v-model for
     two-way binding</p>
</div>

The entire block shows exactly as typed. Vue ignores everything inside!

graph TD A["Vue sees v-pre"] --> B["Skips all children"] B --> C["No compilation"] C --> D["Raw HTML shown"]

Quick Tip đź’ˇ

Use v-pre when you’re building a code documentation site or showing Vue examples to learners!


⏱️ The v-once Directive

What is v-once?

Imagine you paint a picture on the wall. With v-once, that picture is painted ONCE and stays forever — even if you try to repaint it later.

Vue renders the element ONE time, then freezes it like a statue!

Why Use It?

  1. Performance boost — Vue doesn’t watch it anymore
  2. Static content that never changes
  3. Save memory on large pages

Simple Example

<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <!-- This updates normally -->
  <p>Count: {{ count }}</p>

  <!-- This is frozen! -->
  <p v-once>
    Started at: {{ count }}
  </p>

  <button @click="count++">
    Add 1
  </button>
</template>

What happens:

  • First <p> shows: 0, 1, 2, 3… (updates!)
  • Second <p> always shows: “Started at: 0” (frozen!)
graph TD A["Component Mounts"] --> B["v-once renders"] B --> C["Value: 0"] C --> D["User clicks button"] D --> E["count becomes 1, 2, 3..."] E --> F["v-once still shows 0"] F --> G["Frozen forever!"]

When to Use v-once

Use v-once ✅ Don’t Use v-once ❌
Page title Live counters
Copyright text User input forms
Static headers Real-time data
Terms of service Chat messages

⚠️ The v-for with v-if Warning

The Problem

What if you tried to eat breakfast AND brush your teeth at the same time? Messy, right?

That’s what happens when you put v-for and v-if on the same element!

The Rule

v-if has higher priority than v-for!

This means Vue checks v-if FIRST, before even starting the loop.

❌ Wrong Way

<!-- DON'T DO THIS! -->
<li v-for="user in users"
    v-if="user.isActive">
  {{ user.name }}
</li>

Problem: Vue can’t access user when checking v-if because the loop hasn’t started yet!

âś… Right Way #1: Wrap with Template

<template v-for="user in users"
          :key="user.id">
  <li v-if="user.isActive">
    {{ user.name }}
  </li>
</template>

The <template> tag is invisible — it just wraps the loop!

âś… Right Way #2: Use Computed Property

<script setup>
import { computed } from 'vue'

const users = ref([
  { id: 1, name: 'Ali', isActive: true },
  { id: 2, name: 'Bo', isActive: false },
  { id: 3, name: 'Chen', isActive: true }
])

const activeUsers = computed(() => {
  return users.value.filter(
    user => user.isActive
  )
})
</script>

<template>
  <li v-for="user in activeUsers"
      :key="user.id">
    {{ user.name }}
  </li>
</template>

Better! Filter the list BEFORE looping!

graph TD A["Raw Data: All Users"] --> B["Computed: Filter Active"] B --> C["activeUsers Array"] C --> D["v-for loops cleanly"] D --> E["No v-if needed!"]

Why This Matters

Approach Performance Clarity
v-for + v-if same element ❌ Bad ❌ Confusing
template wrapper âś… Good âś… Clear
computed property âś…âś… Best âś…âś… Cleanest

Summary: Your Three New Powers! 🦸

Directive What It Does When to Use
v-pre Skips compilation Show raw Vue syntax
v-once Renders once, frozen Static content
v-for + v-if ⚠️ Don’t mix! Use template or computed

Memory Trick đź§ 

  • v-PRE = PREvent Vue from reading
  • v-ONCE = render ONCE, then sleep
  • v-for + v-if = NEVER on same element!

Practice Time! 🎮

Try this in your own Vue project:

  1. Create a paragraph with v-pre showing {{ hello }}
  2. Make a counter with one v-once frozen value
  3. Filter a list using a computed property

You’ve got this! These special directives are your secret shortcuts to faster, smarter Vue apps! 🚀

Loading story...

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Interactive content is coming soon.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Cheatsheet is coming soon.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Quiz is coming soon.

Flashcard Preview

Flashcard - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Flashcards are coming soon.