Thuta Learning
ရှာဖွေရန်
AdvancedWeb Developmentbeginner

Fetching Data and Async UI

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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

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

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

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 UpVue.js

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

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

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

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

You'll know it worked when: loading → users list OR error message

Fetching Data and Async UI | Thuta Learning