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

Props and One-Way Data Flow

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

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

  • defineProps သုံးရန်
  • Dynamic props bind လုပ်ရန်
  • Props ကိုမပြောင်းသင့်သည့်အကြောင်းနားလည်ရန်

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

Props and One-Way Data Flow ကို ရိုးရိုးလေးနားလည်မယ်

Props သည် parent မှ child သို့ one-way data flow ဖြစ်သည်။ Child က prop ကိုတိုက်ရိုက်မပြောင်းဘဲ ပြောင်းလဲလိုပါက event emit လုပ်ရမည်။ Type, required နှင့် default validation များဖြင့် component contract ကိုရှင်းလင်းစေပါ။

Problem → Why → Solution

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

Child ထဲမှ prop ကိုတိုက်ရိုက် mutate လုပ်ခြင်း

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

Number prop ကို :price မသုံးဘဲ string အဖြစ်ပို့ခြင်း

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

Parent component မှ child component သို့ data ပေးပို့ရန် typed props ကိုအသုံးပြုမည်။

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

defineProps သုံးရန်

အဓိက Syntax / Pattern

vue
<!-- ProductCard.vue -->
<script setup>
const props = defineProps({
  title: { type: String, required: true },
  price: { type: Number, default: 0 }
})
</script>

<template>
  <article>
    <h3>{{ props.title }}</h3>
    <p>{{ props.price.toLocaleString() }} MMK</p>
  </article>
</template>

Parent component မှ child component သို့ data ပေးပို့ရန် typed props ကိုအသုံးပြုမည်။

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

  1. Props / event<!-- ProductCard.vue -->

    Props သည် parent မှ child သို့ one-way data flow ဖြစ်သည်။ Child က prop ကိုတိုက်ရိုက်မပြောင်းဘဲ ပြောင်းလဲလိုပါက event emit လုပ်ရမည်။ Type, required နှင့် default validation များဖြင့် component contract ကိုရှင်းလင်းစေပါ။

  2. Component + template</script>

    Parent component မှ child component သို့ data ပေးပို့ရန် typed props ကိုအသုံးပြုမည်။

  3. Rendered Vue UI</template>

    Props and One-Way Data Flow ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

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

Success

defineProps သုံးရန်

Failure

Child ထဲမှ prop ကိုတိုက်ရိုက် mutate လုပ်ခြင်း

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

Props သည် parent မှ child သို့ one-way data flow ဖြစ်သည်။ Child က prop ကိုတိုက်ရိုက်မပြောင်းဘဲ ပြောင်းလဲလိုပါက event emit လုပ်ရမည်။ Type, required နှင့် default validation များဖြင့် component contract ကိုရှင်းလင်းစေပါ။

vue
<!-- ProductCard.vue -->
<script setup>
const props = defineProps({
  title: { type: String, required: true },
  price: { type: Number, default: 0 }
})
</script>

<template>
  <article>
    <h3>{{ props.title }}</h3>
    <p>{{ props.price.toLocaleString() }} MMK</p>
  </article>
</template>
You should see
Vue Handbook
18,000 MMK

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

UserBadge component ကို name, role နှင့် online Boolean props များဖြင့် တည်ဆောက်ပါ။

Props — Vue.js

Real-world usage

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

Props and One-Way Data Flow ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Mini challenge

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

UserBadge component ကို name, role နှင့် online Boolean props များဖြင့် တည်ဆောက်ပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
vue
<!-- ProductCard.vue -->
<script setup>
const props = defineProps({
  title: { type: String, required: true },
  price: { type: Number, default: 0 }
})
</script>

<template>
  <article>
    <h3>{{ props.title }}</h3>
    <p>{{ props.price.toLocaleString() }} MMK</p>
  </article>
</template>

ရလဒ်: defineProps သုံးရန်

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

Props and One-Way Data Flow ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Quick Quiz 1 / 3

Props and One-Way Data Flow ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Props / event → Component + template → Rendered Vue UI
  • Parent component မှ child component သို့ data ပေးပို့ရန် typed props ကိုအသုံးပြုမည်။
  • Child ထဲမှ prop ကိုတိုက်ရိုက် mutate လုပ်ခြင်း
  • Props and One-Way Data Flow ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Props — Vue.js

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

  • Child ထဲမှ prop ကိုတိုက်ရိုက် mutate လုပ်ခြင်း
  • Number prop ကို :price မသုံးဘဲ string အဖြစ်ပို့ခြင်း

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

UserBadge component ကို name, role နှင့် online Boolean props များဖြင့် တည်ဆောက်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Vue Handbook 18,000 MMK

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

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

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

Props and One-Way Data Flow | Thuta Learning