Thuta Learning
Vue.js
BasicWeb Developmentအခြေခံ

Conditional and List Rendering

Vue.jsသင်ခန်းစာ 720 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • v-if နှင့် v-show ကိုရွေးချယ်ရန်
  • v-for တွင် stable key ထည့်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Conditional and List Rendering ကို ရိုးရိုးလေးနားလည်မယ်

v-if သည် condition false ဖြစ်လျှင် element ကို DOM မှဖယ်ရှားသည်။ v-show သည် CSS display ကိုသာပြောင်းလဲသောကြောင့် မကြာခဏ toggle လုပ်သည့် UI တွင်သင့်တော်သည်။ v-for list တိုင်းတွင် unique :key ထည့်ပါ။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

List index ကို key အဖြစ်သုံးခြင်း

ဖြစ်လေ့ရှိတဲ့အမှား

Element တစ်ခုတည်းပေါ်တွင် v-if နှင့် v-for ကိုအတူသုံးခြင်း

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

v-if, v-show နှင့် v-for ကို အသုံးပြုပြီး condition နှင့် collection အလိုက် UI render လုပ်မည်။

Mental Model
  1. Props / event
  2. Component + template
  3. Rendered Vue UI

v-if နှင့် v-show ကိုရွေးချယ်ရန်

အဓိက Syntax / Pattern

vue
<script setup>
const tasks = [
  { id: 1, title: 'Read Vue guide', done: true },
  { id: 2, title: 'Build a component', done: false }
]
</script>

<template>
  <p v-if="tasks.length === 0">No tasks</p>
  <ul v-else>
    <li v-for="task in tasks" :key="task.id">
      {{ task.done ? '✓' : '○' }} {{ task.title }}
    </li>
  </ul>
</template>

v-if, v-show နှင့် v-for ကို အသုံးပြုပြီး condition နှင့် collection အလိုက် UI render လုပ်မည်။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Props / event<script setup>

    v-if သည် condition false ဖြစ်လျှင် element ကို DOM မှဖယ်ရှားသည်။ v-show သည် CSS display ကိုသာပြောင်းလဲသောကြောင့် မကြာခဏ toggle လုပ်သည့် UI တွင်သင့်တော်သည်။ v-for list တိုင်းတွင် unique :key ထည့်ပါ။

  2. Component + template<p v-if="tasks.length === 0">No tasks</p>

    v-if, v-show နှင့် v-for ကို အသုံးပြုပြီး condition နှင့် collection အလိုက် UI render လုပ်မည်။

  3. Rendered Vue UI</template>

    Conditional and List Rendering ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Execution Flow
  1. Props / event
  2. Component + template
  3. Rendered Vue UI

Success

v-if နှင့် v-show ကိုရွေးချယ်ရန်

Failure

List index ကို key အဖြစ်သုံးခြင်း

ရိုးရိုးလေး ရှင်းပြမယ်

v-if သည် condition false ဖြစ်လျှင် element ကို DOM မှဖယ်ရှားသည်။ v-show သည် CSS display ကိုသာပြောင်းလဲသောကြောင့် မကြာခဏ toggle လုပ်သည့် UI တွင်သင့်တော်သည်။ v-for list တိုင်းတွင် unique :key ထည့်ပါ။

vue
<script setup>
const tasks = [
  { id: 1, title: 'Read Vue guide', done: true },
  { id: 2, title: 'Build a component', done: false }
]
</script>

<template>
  <p v-if="tasks.length === 0">No tasks</p>
  <ul v-else>
    <li v-for="task in tasks" :key="task.id">
      {{ task.done ? '✓' : '○' }} {{ task.title }}
    </li>
  </ul>
</template>
You should see
✓ Read Vue guide
○ Build a component

လက်တွေ့စမ်းကြည့်ရန်

Products list ကို stock > 0 ဖြစ်သော item များသာပြပြီး unique id ကို key အဖြစ်သုံးပါ။

List Rendering — Vue.js

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Conditional and List Rendering ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Products list ကို stock > 0 ဖြစ်သော item များသာပြပြီး unique id ကို key အဖြစ်သုံးပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
vue
<script setup>
const tasks = [
  { id: 1, title: 'Read Vue guide', done: true },
  { id: 2, title: 'Build a component', done: false }
]
</script>

<template>
  <p v-if="tasks.length === 0">No tasks</p>
  <ul v-else>
    <li v-for="task in tasks" :key="task.id">
      {{ task.done ? '✓' : '○' }} {{ task.title }}
    </li>
  </ul>
</template>

ရလဒ်: v-if နှင့် v-show ကိုရွေးချယ်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Conditional and List Rendering ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Quick Quiz 1 / 3

Conditional and List Rendering ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Props / event → Component + template → Rendered Vue UI
  • v-if, v-show နှင့် v-for ကို အသုံးပြုပြီး condition နှင့် collection အလိုက် UI render လုပ်မည်။
  • List index ကို key အဖြစ်သုံးခြင်း
  • Conditional and List Rendering ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

List Rendering — Vue.js

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • List index ကို key အဖြစ်သုံးခြင်း
  • Element တစ်ခုတည်းပေါ်တွင် v-if နှင့် v-for ကိုအတူသုံးခြင်း

လေ့ကျင့်ခန်း

Products list ကို stock > 0 ဖြစ်သော item များသာပြပြီး unique id ကို key အဖြစ်သုံးပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: ✓ Read Vue guide ○ Build a component

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Conditional and List Rendering | Thuta Learning