vue3封裝自己的分頁(yè)組件
本文實(shí)例為大家分享了vue3封裝自己分頁(yè)組件的具體代碼,供大家參考,具體內(nèi)容如下
背景
在瀏覽列表類型的數(shù)據(jù)的時(shí)候,如果數(shù)據(jù)比較多一次性全部請(qǐng)求會(huì)出現(xiàn)性能損耗以及加載延遲等問(wèn)題,那么此時(shí)分頁(yè)組件就起到了關(guān)鍵作用,它可以只請(qǐng)求一部分?jǐn)?shù)據(jù),也不會(huì)占用太多的頁(yè)面空間,想看別的數(shù)據(jù)可以通過(guò)頁(yè)碼的改變來(lái)發(fā)起請(qǐng)求,刷新頁(yè)面數(shù)據(jù)
現(xiàn)在我們自己來(lái)封裝分頁(yè)組件
組件所需參數(shù)
- total 屬性 :用來(lái)傳遞數(shù)據(jù)總條數(shù)
- pagesize 屬性 :每頁(yè)展示幾條數(shù)據(jù)
- currentPage 屬性 :當(dāng)前默認(rèn)頁(yè)碼
- change-page 事件 :頁(yè)碼改變時(shí)觸發(fā)的事件,參數(shù)為當(dāng)前頁(yè)碼
組件落地代碼my-pagination.vue
<template> <div class="my-pagination"> <a href="javascript:;" :class="{ disabled: currentPage === 1 }" @click="changePage(false)">上一頁(yè)</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)">下一頁(yè)</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 }) { // 當(dāng)前頁(yè) const currentPage = ref(attrs.currentPage) // 計(jì)算總頁(yè)數(shù) const pages = computed(() => Math.ceil(props.total / props.pagesize)) // 頁(yè)碼顯示組合 const list = computed(() => { const result = [] // 總頁(yè)數(shù)小于等于5頁(yè)的時(shí)候 if (pages <= 5) { for (let i = 1; i <= pages; i++) { result.push(i) } } else { // 總頁(yè)數(shù)大于5頁(yè)的時(shí)候 // 控制兩個(gè)極端那邊的省略號(hào)的有無(wú),頁(yè)碼的顯示個(gè)數(shù)與選中頁(yè)碼居中 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 }) // 點(diǎn)擊上一頁(yè)下一頁(yè)頁(yè)碼改變頁(yè)碼 const changePage = type => { // 點(diǎn)擊上一頁(yè)按鈕 if (type === false) { if (currentPage.value <= 1) return currentPage.value -= 1 } else if (type === true) { // 點(diǎn)擊下一頁(yè)按鈕 if (currentPage.value >= pages.value) return currentPage.value += 1 } else { // 點(diǎn)擊頁(yè)碼 currentPage.value = type } // 傳給父組件當(dāng)前頁(yè)碼,可以在該事件中做相關(guān)操作 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" />
效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 音樂(lè)App QQ音樂(lè)搜索列表最新接口跨域設(shè)置方法
這篇文章主要介紹了vue 音樂(lè)App QQ音樂(lè)搜索列表最新接口跨域設(shè)置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09解決vue使用vant輪播組件swipe + flex時(shí)文字抖動(dòng)問(wèn)題
這篇文章主要介紹了解決vue使用vant輪播組件swipe + flex時(shí)文字抖動(dòng)問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-01-01vue中this.$router.go(-1)失效(路由改變了,界面未刷新)
本文主要介紹了vue中this.$router.go(-1)失效(路由改變了,界面未刷新),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12vue項(xiàng)目設(shè)置scrollTop不起作用(總結(jié))
這篇文章主要介紹了vue項(xiàng)目設(shè)置scrollTop不起作用(總結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12vue之elementUi的el-select同時(shí)獲取value和label的三種方式
這篇文章主要介紹了vue之elementUi的el-select同時(shí)獲取value和label的三種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue實(shí)現(xiàn)微信分享鏈接添加動(dòng)態(tài)參數(shù)的方法
這篇文章主要介紹了vue微信分享鏈接添加動(dòng)態(tài)參數(shù)的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04vue動(dòng)態(tài)配置模板 ''component is''代碼
這篇文章主要介紹了vue動(dòng)態(tài)配置模板 'component is'代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07