?Electron-Vite?+?Vue?3??項目中實現(xiàn)流暢觸底加載更多功能
在 ?Electron-Vite + Vue 3? 項目中實現(xiàn)觸底加載更多(無限滾動)功能,可以通過監(jiān)聽滾動事件或使用現(xiàn)成的庫(如 vue-infinite-loading)來完成。以下是詳細實現(xiàn)步驟:
?方案一:原生滾動監(jiān)聽(推薦)??
?1. 模板部分?
在需要觸底加載的列表組件中,添加滾動監(jiān)聽:
<template>
<div class="scroll-container" @scroll="handleScroll">
<div v-for="item in items" :key="item.id">{{ item.content }}</div>
<div v-if="loading" class="loading">加載中...</div>
<div v-if="noMore" class="no-more">沒有更多了</div>
</div>
</template>?2. 腳本部分?
<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
const items = ref([]); // 數(shù)據(jù)列表
const page = ref(1); // 當前頁碼
const loading = ref(false);
const noMore = ref(false);
// 模擬異步加載數(shù)據(jù)
const loadMore = async () => {
if (loading.value || noMore.value) return;
loading.value = true;
try {
// 替換為實際API請求
const newItems = await fetchData(page.value);
if (newItems.length === 0) {
noMore.value = true;
} else {
items.value = [...items.value, ...newItems];
page.value++;
}
} finally {
loading.value = false;
}
};
// 監(jiān)聽滾動事件
const handleScroll = (e) => {
const { scrollTop, scrollHeight, clientHeight } = e.target;
// 觸底條件:距離底部 < 50px
if (scrollHeight - (scrollTop + clientHeight) < 50) {
loadMore();
}
};
// 初始化加載
onMounted(() => {
loadMore();
});
// 清理事件(如果綁定在window上)
onUnmounted(() => {
window.removeEventListener('scroll', handleScroll);
});
</script>?3. 樣式部分?
<style scoped>
.scroll-container {
height: 80vh; /* 固定高度觸發(fā)滾動 */
overflow-y: auto;
}
.loading, .no-more {
text-align: center;
padding: 10px;
}
</style>?方案二:使用vue-infinite-loading庫?
?1. 安裝依賴?
npm install vue-infinite-loading@next
?2. 在組件中使用?
<template>
<div>
<div v-for="item in items" :key="item.id">{{ item.content }}</div>
<InfiniteLoading @infinite="loadMore" :distance="100">
<template #spinner>加載中...</template>
<template #no-more>沒有更多了</template>
</InfiniteLoading>
</div>
</template>
<script setup>
import { ref } from 'vue';
import InfiniteLoading from 'vue-infinite-loading';
const items = ref([]);
const page = ref(1);
const noMore = ref(false);
const loadMore = async ($state) => {
try {
const newItems = await fetchData(page.value);
if (newItems.length === 0) {
noMore.value = true;
$state.complete();
} else {
items.value.push(...newItems);
page.value++;
$state.loaded();
}
} catch (err) {
$state.error();
}
};
</script>?關鍵細節(jié)說明?
- ?觸底條件計算?
scrollHeight:內(nèi)容總高度scrollTop:已滾動高度clientHeight:容器可視高度- 公式:
scrollHeight - (scrollTop + clientHeight) < threshold
- ?Electron 特殊處理?
- 如果內(nèi)容在
<webview>或<iframe>中,需在 ?主進程? 中允許加載外部資源:
new BrowserWindow({
webPreferences: {
webSecurity: false // 允許跨域請求(開發(fā)環(huán)境)
}
});- ?性能優(yōu)化?
- 使用 ?防抖?(
lodash.debounce)避免頻繁觸發(fā):
import { debounce } from 'lodash-es';
const handleScroll = debounce((e) => { ... }, 200);?完整示例項目結(jié)構(gòu)?
electron-vite-vue3-project/ ├── src/ │ ├── main/ # Electron 主進程代碼 │ ├── renderer/ # Vue 渲染進程代碼 │ │ └── components/ │ │ └── InfiniteScroll.vue # 觸底加載組件 │ └── preload.js └── vite.config.js
?常見問題?
- ?Q:滾動事件不觸發(fā)???
A:確保容器有固定高度和overflow-y: auto。 - ?Q:數(shù)據(jù)重復加載???
A:檢查loading和noMore的狀態(tài)鎖。 - ?Q:Electron 中跨域請求失?????
A:在主進程配置webSecurity: false(僅限開發(fā)環(huán)境)。
通過以上方法,可以輕松在 Electron-Vite + Vue 3 中實現(xiàn)流暢的觸底加載效果!
到此這篇關于?Electron-Vite + Vue 3? 項目中實現(xiàn)觸底加載更多的文章就介紹到這了,更多相關vue觸底加載更多內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vuex實現(xiàn)數(shù)據(jù)持久化的兩種方案
這兩天在做vue項目存儲個人信息的時候,遇到了頁面刷新后個人信息數(shù)據(jù)丟失的問題,在查閱資料后,我得出兩種解決數(shù)據(jù)丟失,使用數(shù)據(jù)持久化的方法,感興趣的小伙伴跟著小編一起來看看吧2023-08-08
vue+elementUI顯示表格指定列合計數(shù)據(jù)方式
這篇文章主要介紹了vue+elementUI顯示表格指定列合計數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue解決element-ui消息提示$message重疊問題
這篇文章主要為大家介紹了Vue解決element-ui消息提示$message重疊問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08

