vue3解決各場(chǎng)景l(fā)oading過(guò)度的五種方法
1. 使用 v-if 和 v-show
使用場(chǎng)景:當(dāng)需要根據(jù)條件顯示或隱藏某個(gè)元素時(shí),可以使用 v-if 和 v-show。
優(yōu)點(diǎn):代碼簡(jiǎn)潔易懂,執(zhí)行效率高。
缺點(diǎn):當(dāng)條件判斷比較復(fù)雜時(shí),v-if 和 v-show 的嵌套層數(shù)過(guò)多,可讀性差。
示例代碼:
<template>
<div>
<div v-if="isLoading">加載中...</div>
<div v-else>內(nèi)容</div>
</div>
</template>
<script>
export default {
data() {
return {
isLoading: true,
};
},
mounted() {
setTimeout(() => {
this.isLoading = false;
}, 2000);
},
};
</script>
2. 使用路由守衛(wèi)(Navigation Guards)
使用場(chǎng)景:當(dāng)需要在路由切換前后執(zhí)行一些操作時(shí),可以使用路由守衛(wèi)。
優(yōu)點(diǎn):可以確保在路由切換過(guò)程中執(zhí)行一些特定的邏輯,例如顯示 loading 動(dòng)畫。
缺點(diǎn):需要配合 Vue Router 使用,配置相對(duì)復(fù)雜。
示例代碼:
import { createRouter, createWebHistory } from 'vue-router';
const routes = [
{
path: '/',
name: 'Home',
component: () => import('@/views/Home.vue'),
},
];
const router = createRouter({
history: createWebHistory(),
routes,
});
router.beforeEach((to, from, next) => {
if (to.path === '/') {
next();
} else {
next({ path: '/' });
}
});
export default router;
3. 使用異步組件(Async Components)
使用場(chǎng)景:當(dāng)需要?jiǎng)討B(tài)加載一個(gè)組件時(shí),可以使用異步組件。
優(yōu)點(diǎn):可以提高應(yīng)用程序的性能,因?yàn)橹挥性趯?shí)際需要時(shí)才會(huì)加載組件。
缺點(diǎn):需要對(duì)異步組件有一定的了解,才能正確地使用它們。
示例代碼:
<template>
<div>
<AsyncComponent :is="isLoading" />
</div>
</template>
<script>
import { defineAsyncComponent } from 'vue';
export default {
data() {
return {
isLoading: true,
};
},
components: {
AsyncComponent: defineAsyncComponent(() => import('./AsyncComponent.vue')),
},
mounted() {
setTimeout(() => {
this.isLoading = false;
}, 2000);
},
};
</script>
4. 使用第三方庫(kù)(如 element-plus、vant 等)提供的 loading 組件
使用場(chǎng)景:當(dāng)需要提供更好的用戶體驗(yàn)時(shí),可以使用第三方庫(kù)提供的 loading 組件。
優(yōu)點(diǎn):可以提供更美觀、更易于使用的 loading 組件。
缺點(diǎn):可能會(huì)增加項(xiàng)目的體積。
示例代碼:
<template>
<div>
<el-loading v-if="isLoading" fullscreen></el-loading>
<div v-else>內(nèi)容</div>
</div>
</template>
<script>
import { ElLoading } from 'element-plus';
export default {
data() {
return {
isLoading: true,
};
},
mounted() {
setTimeout(() => {
this.isLoading = false;
}, 2000);
},
components: {
ElLoading,
},
};
</script>
5. 自定義全局的 loading 組件
使用場(chǎng)景:當(dāng)需要提供更好的用戶體驗(yàn)時(shí),可以使用自定義的全局 loading 組件。
優(yōu)點(diǎn):可以提供更美觀、更易于使用的 loading 組件。此外,自定義的 loading 組件可以更好地控制其顯示和隱藏。
缺點(diǎn):需要一定的開發(fā)成本。
MyLoading 的自定義組件:
<template>
<div>
<my-loading v-if="isLoading"></my-loading>
<div v-else>內(nèi)容</div>
</div>
</template>
<script>
import MyLoading from './MyLoading.vue';
export default {
data() {
return {
isLoading: true,
};
},
mounted() {
setTimeout(() => {
this.isLoading = false;
}, 2000);
},
components: {
MyLoading,
},
};
</script>
到此這篇關(guān)于vue3解決各場(chǎng)景l(fā)oading過(guò)度的五種方法的文章就介紹到這了,更多相關(guān)vue3解決loading過(guò)度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
完美解決通過(guò)IP地址訪問(wèn)VUE項(xiàng)目的問(wèn)題
這篇文章主要介紹了完美解決通過(guò)IP地址訪問(wèn)VUE項(xiàng)目的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue中使用定時(shí)器(setInterval、setTimeout)的兩種方式
js中定時(shí)器有兩種,一個(gè)是循環(huán)執(zhí)行?setInterval,另一個(gè)是定時(shí)執(zhí)行?setTimeout,這篇文章主要介紹了Vue中使用定時(shí)器?(setInterval、setTimeout)的兩種方式,需要的朋友可以參考下2023-03-03
Element Cascader 級(jí)聯(lián)選擇器的使用示例
這篇文章主要介紹了Element Cascader 級(jí)聯(lián)選擇器的使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue項(xiàng)目實(shí)戰(zhàn)之圓柱狀水波效果實(shí)現(xiàn)
最近工作中實(shí)現(xiàn)的一個(gè)效果不錯(cuò),分享給大家,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目實(shí)戰(zhàn)之圓柱狀水波效果實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Vue 將后臺(tái)傳過(guò)來(lái)的帶html字段的字符串轉(zhuǎn)換為 HTML
這篇文章主要介紹了Vue 將后臺(tái)傳過(guò)來(lái)的帶html字段的字符串轉(zhuǎn)換為 HTML ,需要的朋友可以參考下2018-03-03
使用Vue完成一個(gè)簡(jiǎn)單的todolist的方法
本篇文章主要介紹了使用Vue完成一個(gè)簡(jiǎn)單的todolist的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

