vue3之Suspense加載異步數(shù)據(jù)的使用
Suspense使用
<template>
<Suspense>
<template #default>
<ProductList></ProductList>
</template>
<template #fallback> <div>loading...</div> </template>
</Suspense>
</template>
<script setup lang="ts" name="Cart">
import ProductList from "./ProductList.vue";
</script>
<style lang="scss" scoped></style>
組件
使用 flag 與 Promise 來模擬異步加載數(shù)據(jù),渲染成功與失敗的頁面效果
<!-- -->
<template>
<div v-if="data">
ProductList
<div>data父 - {{ data }}</div>
</div>
<div v-if="err">
{{ err }}
</div>
</template>
<script setup lang="ts" name="ProductList">
import { ref } from "vue";
const data = ref<any>(null);
const flag = false;
const err = ref(null);
function aaa() {
return new Promise((resolve) => {
setTimeout(() => {
if (!flag) {
return resolve({ code: 0, errorMsg: "參數(shù)錯(cuò)誤" });
}
return resolve({
code: 200,
data: {
result: 42,
},
});
}, 3000);
});
}
const res = await aaa();
console.log(res);
if (res.code === 200) {
data.value = res.data.result;
} else {
data.value = "";
err.value = res.errorMsg;
}
</script>
<style lang="scss" scoped></style>
效果
調(diào)用請(qǐng)求的 loading效果

請(qǐng)求 返回?cái)?shù)據(jù)時(shí)候

請(qǐng)求 返回錯(cuò)誤時(shí)候

到此這篇關(guān)于vue3之Suspense加載異步數(shù)據(jù)的使用的文章就介紹到這了,更多相關(guān)vue3 Suspense加載異步內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue以組件或者插件的形式實(shí)現(xiàn)throttle或者debounce
這篇文章主要介紹了vue以組件或者插件的形式實(shí)現(xiàn)throttle或者debounce,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
vue-cli3使用mock數(shù)據(jù)的方法分析
這篇文章主要介紹了vue-cli3使用mock數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了vue-cli3使用mock數(shù)據(jù)的相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
vue-cli或vue項(xiàng)目利用HBuilder打包成移動(dòng)端app操作
這篇文章主要介紹了vue-cli或vue項(xiàng)目利用HBuilder打包成移動(dòng)端app操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
淺談el-table中使用虛擬列表對(duì)對(duì)表格進(jìn)行優(yōu)化
我們會(huì)經(jīng)常使用表格,如果數(shù)據(jù)量大就直接可以分頁,如果多條可能會(huì)影響表格的卡頓,那么應(yīng)該如何進(jìn)行優(yōu)化,感興趣的可以了解一下2021-08-08

