vue3封裝自己的分頁組件
本文實例為大家分享了vue3封裝自己分頁組件的具體代碼,供大家參考,具體內容如下
背景
在瀏覽列表類型的數(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" />
效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決vue使用vant輪播組件swipe + flex時文字抖動問題
這篇文章主要介紹了解決vue使用vant輪播組件swipe + flex時文字抖動問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-01-01vue中this.$router.go(-1)失效(路由改變了,界面未刷新)
本文主要介紹了vue中this.$router.go(-1)失效(路由改變了,界面未刷新),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-12-12vue之elementUi的el-select同時獲取value和label的三種方式
這篇文章主要介紹了vue之elementUi的el-select同時獲取value和label的三種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02vue實現(xiàn)微信分享鏈接添加動態(tài)參數(shù)的方法
這篇文章主要介紹了vue微信分享鏈接添加動態(tài)參數(shù)的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-04-04vue動態(tài)配置模板 ''component is''代碼
這篇文章主要介紹了vue動態(tài)配置模板 'component is'代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07