vue3封裝自己的分頁組件
本文實例為大家分享了vue3封裝自己分頁組件的具體代碼,供大家參考,具體內(nèi)容如下
背景
在瀏覽列表類型的數(shù)據(jù)的時候,如果數(shù)據(jù)比較多一次性全部請求會出現(xiàn)性能損耗以及加載延遲等問題,那么此時分頁組件就起到了關鍵作用,它可以只請求一部分數(shù)據(jù),也不會占用太多的頁面空間,想看別的數(shù)據(jù)可以通過頁碼的改變來發(fā)起請求,刷新頁面數(shù)據(jù)
現(xiàn)在我們自己來封裝分頁組件
組件所需參數(shù)
- total 屬性 :用來傳遞數(shù)據(jù)總條數(shù)
- pagesize 屬性 :每頁展示幾條數(shù)據(jù)
- currentPage 屬性 :當前默認頁碼
- change-page 事件 :頁碼改變時觸發(fā)的事件,參數(shù)為當前頁碼
組件落地代碼my-pagination.vue
<template>
<div class="my-pagination">
<a href="javascript:;" :class="{ disabled: currentPage === 1 }" @click="changePage(false)">上一頁</a>
<span v-if="currentPage > 3">...</span>
<a
href="javascript:;"
v-for="item in list"
:key="item"
:class="{ active: currentPage === item }"
@click="changePage(item)"
>{{ item }}</a
>
<span v-if="currentPage < pages - 2">...</span>
<a href="javascript:;" :class="{ disabled: currentPage === pages }" @click="changePage(true)">下一頁</a>
</div>
</template>
<script>
import { computed, ref } from 'vue-demi'
export default {
name: 'MyPagination',
props: {
total: {
type: Number,
default: 80
},
pagesize: {
type: Number,
default: 10
}
},
setup(props, { emit, attrs }) {
// 當前頁
const currentPage = ref(attrs.currentPage)
// 計算總頁數(shù)
const pages = computed(() => Math.ceil(props.total / props.pagesize))
// 頁碼顯示組合
const list = computed(() => {
const result = []
// 總頁數(shù)小于等于5頁的時候
if (pages <= 5) {
for (let i = 1; i <= pages; i++) {
result.push(i)
}
} else {
// 總頁數(shù)大于5頁的時候
// 控制兩個極端那邊的省略號的有無,頁碼的顯示個數(shù)與選中頁碼居中
if (currentPage.value <= 2) {
for (let i = 1; i <= 5; i++) {
result.push(i)
}
} else if (currentPage.value >= 3 && currentPage.value <= pages.value - 2) {
for (let i = currentPage.value - 2; i <= currentPage.value + 2; i++) {
result.push(i)
}
} else if (currentPage.value > pages.value - 2) {
for (let i = pages.value - 4; i <= pages.value; i++) {
result.push(i)
}
}
}
return result
})
// 點擊上一頁下一頁頁碼改變頁碼
const changePage = type => {
// 點擊上一頁按鈕
if (type === false) {
if (currentPage.value <= 1) return
currentPage.value -= 1
} else if (type === true) {
// 點擊下一頁按鈕
if (currentPage.value >= pages.value) return
currentPage.value += 1
} else {
// 點擊頁碼
currentPage.value = type
}
// 傳給父組件當前頁碼,可以在該事件中做相關操作
emit('change-page', currentPage.value)
}
return { currentPage, pages, list, changePage }
}
}
</script>
<style scoped lang="less">
.my-pagination {
display: flex;
justify-content: center;
padding: 30px;
> a {
display: inline-block;
padding: 5px 10px;
border: 1px solid #e4e4e4;
border-radius: 4px;
margin-right: 10px;
&:hover {
color: @xtxColor;
}
&.active {
background: @xtxColor;
color: #fff;
border-color: @xtxColor;
}
&.disabled {
cursor: not-allowed;
opacity: 0.4;
&:hover {
color: #333;
}
}
}
> span {
margin-right: 10px;
}
}
</style>
使用組件
<XtxPagination :total="total" :pagesize="reqParams.pagesize" :currentPage="1" @change-page="changePage" />
效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue如何監(jiān)測數(shù)組類型數(shù)據(jù)發(fā)生改變的(推薦)
這篇文章主要介紹了Vue如何監(jiān)測數(shù)組類型數(shù)據(jù)發(fā)生改變的,本文通過實例代碼圖文詳解給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11
vue-cli 環(huán)境變量 process.env的使用及說明
這篇文章主要介紹了vue-cli 環(huán)境變量 process.env的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

