ရိုးရိုးလေး ရှင်းပြမယ်
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