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

Fetching Data and Async UI

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

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

  • Async request ပြုလုပ်ရန်
  • Loading/error/empty states ပြရန်
  • Unmount တွင် request cancel လုပ်ရန်

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

Fetching Data and Async UI ကို ရိုးရိုးလေးနားလည်မယ်

Network request တိုင်းတွင် success တစ်မျိုးတည်းမဟုတ်ဘဲ loading, error နှင့် empty states ပါရမည်။ response.ok ကိုစစ်ပြီး try/catch/finally သုံးပါ။ Component unmount ဖြစ်သွားပါက AbortController ဖြင့် မလိုအပ်တော့သော request ကို cancel လုပ်နိုင်သည်။

Problem → Why → Solution

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

response.ok မစစ်ခြင်း

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

Loading state ကို error ဖြစ်သည့်အခါ false မပြောင်းခြင်း

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

fetch, async/await, loading, error နှင့် cancellation state များဖြင့် ခိုင်မာသော data UI တည်ဆောက်မည်။

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

Async request ပြုလုပ်ရန်

အဓိက Syntax / Pattern

vue
<script setup>
import { ref, onMounted } from 'vue'
const users = ref([])
const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    const response = await fetch('/api/users')
    if (!response.ok) throw new Error('Request failed')
    users.value = await response.json()
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : 'Unknown error'
  } finally {
    loading.value = false
  }
})
</script>

fetch, async/await, loading, error နှင့် cancellation state များဖြင့် ခိုင်မာသော data UI တည်ဆောက်မည်။

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

  1. Props / event<script setup>

    Network request တိုင်းတွင် success တစ်မျိုးတည်းမဟုတ်ဘဲ loading, error နှင့် empty states ပါရမည်။ response.ok ကိုစစ်ပြီး try/catch/finally သုံးပါ။ Component unmount ဖြစ်သွားပါက AbortController ဖြင့် မလိုအပ်တော့သော request ကို cancel လုပ်နိုင်သည်။

  2. Component + templateif (!response.ok) throw new Error('Request failed')

    fetch, async/await, loading, error နှင့် cancellation state များဖြင့် ခိုင်မာသော data UI တည်ဆောက်မည်။

  3. Rendered Vue UI</script>

    Fetching Data and Async UI ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

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

Success

Async request ပြုလုပ်ရန်

Failure

response.ok မစစ်ခြင်း

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

Network request တိုင်းတွင် success တစ်မျိုးတည်းမဟုတ်ဘဲ loading, error နှင့် empty states ပါရမည်။ response.ok ကိုစစ်ပြီး try/catch/finally သုံးပါ။ Component unmount ဖြစ်သွားပါက AbortController ဖြင့် မလိုအပ်တော့သော request ကို cancel လုပ်နိုင်သည်။

vue
<script setup>
import { ref, onMounted } from 'vue'
const users = ref([])
const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    const response = await fetch('/api/users')
    if (!response.ok) throw new Error('Request failed')
    users.value = await response.json()
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : 'Unknown error'
  } finally {
    loading.value = false
  }
})
</script>
You should see
loading → users list OR error message

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

Public API တစ်ခုမှ data ယူပြီး loading, error, empty နှင့် success UI လေးမျိုးလုံးပြပါ။

Vue — Scaling Up — Vue.js

Real-world usage

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

Fetching Data and Async UI ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Mini challenge

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

Public API တစ်ခုမှ data ယူပြီး loading, error, empty နှင့် success UI လေးမျိုးလုံးပြပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
vue
<script setup>
import { ref, onMounted } from 'vue'
const users = ref([])
const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    const response = await fetch('/api/users')
    if (!response.ok) throw new Error('Request failed')
    users.value = await response.json()
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : 'Unknown error'
  } finally {
    loading.value = false
  }
})
</script>

ရလဒ်: Async request ပြုလုပ်ရန်

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

Fetching Data and Async UI ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Quick Quiz 1 / 3

Fetching Data and Async UI ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Props / event → Component + template → Rendered Vue UI
  • fetch, async/await, loading, error နှင့် cancellation state များဖြင့် ခိုင်မာသော data UI တည်ဆောက်မည်။
  • response.ok မစစ်ခြင်း
  • Fetching Data and Async UI ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။

Vue — Scaling Up — Vue.js

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

  • response.ok မစစ်ခြင်း
  • Loading state ကို error ဖြစ်သည့်အခါ false မပြောင်းခြင်း

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

Public API တစ်ခုမှ data ယူပြီး loading, error, empty နှင့် success UI လေးမျိုးလုံးပြပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: loading → users list OR error message

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

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

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

Fetching Data and Async UI | Thuta Learning