vue3.0手動封裝分頁組件的方法
本文實(shí)例為大家分享了vue3.0手動封裝分頁組件的具體代碼,供大家參考,具體內(nèi)容如下
1.父組件引入
src/views/goods/components/goods-comment.vue
<!-- page表示初始化分頁時,默認(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)式的 // 動態(tài)計算中期的頁碼信息 // 每頁的條數(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) // 動態(tài)計算頁碼列表 const list = computed(() => { // 當(dāng)父組件傳遞total的值發(fā)生變化時,計算屬性會重新計算 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) { // 上一頁 // 頁面是第一頁時,禁止點(diǎn)擊操作 if (currentPage.value === 1) return if (currentPage.value > 1) { currentPage.value -= 1 } } else if (type === true) { // 下一頁 // 頁面是最后頁時,禁止點(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>
知識點(diǎn):attrs表示父組件傳遞的屬性,但是props沒有接收的屬性,這種屬性不是響應(yīng)式的(vue3新增)
3.實(shí)現(xiàn)效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue+elementui通用彈窗的實(shí)現(xiàn)(新增+編輯)
這篇文章主要介紹了vue+elementui通用彈窗的實(shí)現(xiàn)(新增+編輯),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01vue項目tween方法實(shí)現(xiàn)返回頂部的示例代碼
這篇文章主要介紹了vue項目tween方法實(shí)現(xiàn)返回頂部,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03關(guān)于導(dǎo)入、配置Vuetify遇到的幾個問題
這篇文章主要介紹了關(guān)于導(dǎo)入、配置Vuetify遇到的幾個問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06關(guān)于element-ui中el-form自定義驗(yàn)證(調(diào)用后端接口)
這篇文章主要介紹了關(guān)于element-ui中el-form自定義驗(yàn)證(調(diào)用后端接口),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07