ရိုးရိုးလေး ရှင်းပြမယ်
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 တည်ဆောက်မည်။
- Props / event
- Component + template
- Rendered Vue UI
Async request ပြုလုပ်ရန်
အဓိက Syntax / Pattern
<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 ကို အဆင့်လိုက်ဖတ်မယ်
- Props / event
<script setup>Network request တိုင်းတွင် success တစ်မျိုးတည်းမဟုတ်ဘဲ loading, error နှင့် empty states ပါရမည်။ response.ok ကိုစစ်ပြီး try/catch/finally သုံးပါ။ Component unmount ဖြစ်သွားပါက AbortController ဖြင့် မလိုအပ်တော့သော request ကို cancel လုပ်နိုင်သည်။
- Component + template
if (!response.ok) throw new Error('Request failed')fetch, async/await, loading, error နှင့် cancellation state များဖြင့် ခိုင်မာသော data UI တည်ဆောက်မည်။
- Rendered Vue UI
</script>Fetching Data and Async UI ကို example အသေးတစ်ခုနဲ့စပြီး normal, edge နဲ့ failure cases ကိုစစ်ပါ။
- Props / event
- Component + template
- 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 လုပ်နိုင်သည်။
<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>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 ကိုခန့်မှန်းပါ
<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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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