vue3.0手動(dòng)封裝分頁組件的方法
本文實(shí)例為大家分享了vue3.0手動(dòng)封裝分頁組件的具體代碼,供大家參考,具體內(nèi)容如下
1.父組件引入
src/views/goods/components/goods-comment.vue
<!-- page表示初始化分頁時(shí),默認(rèn)顯示第幾頁 -->
<XtxPagination @change-page='changePage' :pagesize='reqParams.pageSize' :total='total' :page='1' />
//調(diào)接口
import {findCommentListByGoods } from '@/api/product.js'
export default{
setup(){
// 篩選條件準(zhǔn)備
const reqParams = reactive({
// 當(dāng)前頁碼
page: 1,
// 每頁的條數(shù)
pageSize: 10,
// 是否有圖片
hasPicture: null,
// 篩選條件
tag: null,
// 排序的字段
sortField: null
})
// 偵聽參數(shù)的變化
watch(reqParams, () => {
findCommentListByGoods(goods.value.id, reqParams).then(ret => {
total.value = ret.result.counts
list.value = ret.result.items
})
}, {
immediate: true
})
// 控制頁碼的變化
const changePage = (page) => {
// 修改分頁參數(shù),重新調(diào)用接口即可
reqParams.page = page
}
}
}
2.子組件
src/components/library/xtx-pagination.vue
<template>
<div class="xtx-pagination">
<a @click='changePage(false)' href="javascript:;" :class="{disabled: currentPage===1}">上一頁</a>
<span v-if='currentPage > 3'>...</span>
<a @click='changePage(item)' href="javascript:;" :class='{active: currentPage===item}' v-for='item in list' :key='item'>{{item}}</a>
<span v-if='currentPage < pages - 2'>...</span>
<a @click='changePage(true)' href="javascript:;" :class='{disabled: currentPage===pages}'>下一頁</a>
</div>
</template>
<script>
import { computed, ref } from 'vue'
export default {
name: 'XtxPagination',
props: {
total: {
type: Number,
default: 80
},
pagesize: {
type: Number,
default: 10
}
// 默認(rèn)初始頁碼
// page: {
// type: Number,
// default: 1
// }
},
setup (props, { emit, attrs }) {
// attrs表示父組件傳遞的屬性,但是props沒有接收的屬性,這種屬性不是響應(yīng)式的
// 動(dòng)態(tài)計(jì)算中期的頁碼信息
// 每頁的條數(shù)
// const pagesize = 8
// 總頁數(shù)
let pages = Math.ceil(props.total / props.pagesize)
// 當(dāng)前頁碼
// console.log(attrs.page)
const currentPage = ref(attrs.page || 1)
// 動(dòng)態(tài)計(jì)算頁碼列表
const list = computed(() => {
// 當(dāng)父組件傳遞total的值發(fā)生變化時(shí),計(jì)算屬性會(huì)重新計(jì)算
pages = Math.ceil(props.total / props.pagesize)
const result = []
// 總頁碼小于等于5;大于5
if (pages <= 5) {
// 總頁碼小于等于5的情況
for (let i = 1; i <= pages; i++) {
result.push(i)
}
} else {
// 總頁碼大于5
if (currentPage.value <= 2) {
// 左側(cè)臨界值
for (let i = 1; i <= 5; i++) {
result.push(i)
}
} else if (currentPage.value >= pages - 1) {
// 右側(cè)臨界值
for (let i = pages - 4; i <= pages; i++) {
result.push(i)
}
} else {
// 中間的狀態(tài)
for (let i = currentPage.value - 2; i <= currentPage.value + 2; i++) {
result.push(i)
}
}
}
return result
})
// 控制上一頁和下一頁變化
const changePage = (type) => {
if (type === false) {
// 上一頁
// 頁面是第一頁時(shí),禁止點(diǎn)擊操作
if (currentPage.value === 1) return
if (currentPage.value > 1) {
currentPage.value -= 1
}
} else if (type === true) {
// 下一頁
// 頁面是最后頁時(shí),禁止點(diǎn)擊操作
if (currentPage.value === pages) return
if (currentPage.value < pages) {
currentPage.value += 1
}
} else {
// 點(diǎn)擊頁碼
currentPage.value = type
}
emit('change-page', currentPage.value)
}
return { list, currentPage, pages, changePage }
}
}
</script>
<style scoped lang="less">
.xtx-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>
知識(shí)點(diǎn):attrs表示父組件傳遞的屬性,但是props沒有接收的屬性,這種屬性不是響應(yīng)式的(vue3新增)
3.實(shí)現(xiàn)效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue觸發(fā)真實(shí)的點(diǎn)擊事件跟用戶行為一致問題
這篇文章主要介紹了vue觸發(fā)真實(shí)的點(diǎn)擊事件跟用戶行為一致問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
VueJS實(shí)現(xiàn)用戶管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了VueJS實(shí)現(xiàn)用戶管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能
這篇文章主要介紹了vue前端實(shí)現(xiàn)驗(yàn)證碼登錄功能,登錄時(shí)圖形驗(yàn)證通過三種方法結(jié)合實(shí)例代碼給大家講解的非常詳細(xì), 通過實(shí)例代碼介紹了vue登錄時(shí)圖形驗(yàn)證碼功能的實(shí)現(xiàn),感興趣的朋友一起看看吧2023-12-12
詳解利用eventemitter2實(shí)現(xiàn)Vue組件通信
這篇文章主要介紹了詳解利用eventemitter2實(shí)現(xiàn)Vue組件通信,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue-cli3啟動(dòng)服務(wù)如何自動(dòng)打開瀏覽器配置
這篇文章主要介紹了vue-cli3啟動(dòng)服務(wù)如何自動(dòng)打開瀏覽器配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue使用webPack打包發(fā)布后頁面顯示空白的解決
這篇文章主要介紹了vue使用webPack打包發(fā)布后頁面顯示空白的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

