亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3.0手動封裝分頁組件的方法

 更新時間:2021年09月26日 11:25:55   作者:勇敢牛牛,沖沖沖  
這篇文章主要為大家詳細介紹了vue3.0手動封裝分頁組件的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue3.0手動封裝分頁組件的具體代碼,供大家參考,具體內(nèi)容如下

1.父組件引入

src/views/goods/components/goods-comment.vue

<!-- page表示初始化分頁時,默認顯示第幾頁 -->
    <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
    }
    // 默認初始頁碼
    // 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) {
        // 上一頁
        // 頁面是第一頁時,禁止點擊操作
        if (currentPage.value === 1) return
        if (currentPage.value > 1) {
          currentPage.value -= 1
        }
      } else if (type === true) {
        // 下一頁
        // 頁面是最后頁時,禁止點擊操作
        if (currentPage.value === pages) return
        if (currentPage.value < pages) {
          currentPage.value += 1
        }
      } else {
        // 點擊頁碼
        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>

知識點:attrs表示父組件傳遞的屬性,但是props沒有接收的屬性,這種屬性不是響應(yīng)式的(vue3新增)

3.實現(xiàn)效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue+elementui通用彈窗的實現(xiàn)(新增+編輯)

    vue+elementui通用彈窗的實現(xiàn)(新增+編輯)

    這篇文章主要介紹了vue+elementui通用彈窗的實現(xiàn)(新增+編輯),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • vue和react項目中key的作用示例詳解

    vue和react項目中key的作用示例詳解

    這篇文章主要為大家介紹了vue和react項目中key的作用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • vue實現(xiàn)登錄時圖形驗證碼

    vue實現(xiàn)登錄時圖形驗證碼

    這篇文章主要為大家詳細介紹了vue實現(xiàn)登錄時圖形驗證碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vue如何循環(huán)提取對象數(shù)組中的值

    Vue如何循環(huán)提取對象數(shù)組中的值

    這篇文章主要介紹了Vue如何循環(huán)提取對象數(shù)組中的值,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • vue項目tween方法實現(xiàn)返回頂部的示例代碼

    vue項目tween方法實現(xiàn)返回頂部的示例代碼

    這篇文章主要介紹了vue項目tween方法實現(xiàn)返回頂部,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • vue 自定義全局方法,在組件里面的使用介紹

    vue 自定義全局方法,在組件里面的使用介紹

    下面小編就為大家介紹一下vue 自定義全局方法,在組件里面的使用。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • vue-cli 3.0 引入mint-ui報錯問題及解決

    vue-cli 3.0 引入mint-ui報錯問題及解決

    這篇文章主要介紹了vue-cli 3.0 引入mint-ui報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 關(guān)于導(dǎo)入、配置Vuetify遇到的幾個問題

    關(guān)于導(dǎo)入、配置Vuetify遇到的幾個問題

    這篇文章主要介紹了關(guān)于導(dǎo)入、配置Vuetify遇到的幾個問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Vue利用插件實現(xiàn)打印功能的示例詳解

    Vue利用插件實現(xiàn)打印功能的示例詳解

    這篇文章主要為大家詳細介紹了Vue如何利用vue-print-nb插件實現(xiàn)打印功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)一下
    2023-03-03
  • 關(guān)于element-ui中el-form自定義驗證(調(diào)用后端接口)

    關(guān)于element-ui中el-form自定義驗證(調(diào)用后端接口)

    這篇文章主要介紹了關(guān)于element-ui中el-form自定義驗證(調(diào)用后端接口),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評論