Vue項目 封裝Element-ui中的el-pagination作為公用分頁組件


原因:分頁在項目當中使用非常頻繁,因此就將el-pagination封裝為了一個全局組件

 

話不多說直接上代碼

 

1.首先在components下面新建一個pagination.vue文件

代碼如下:

查看代碼
<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-sizes="pagesizes"
      :page-size.sync="pageSize"
      :pager-count="pagerCount"
      :layout="layout"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  data() {
    return {}
  },
  props: {
    /**
     * 總頁數
     */
    total: {
      required: true,
      type: Number
    },
    /**
     * 默認當前頁
     */
    page: {
      default: 1,
      type: Number
    },
    /**
     * 默認分頁大小
     */
    limit: {
      type: Number,
      default: 5
    },
    /**
     * 分頁大小
     */
    pagesizes: {
      type: Array,
      default() {
        return [5, 10, 20, 30, 50, 100]
      }
    },
    /**
     * 移動端頁碼按鈕的數量端默認值5
     */
    pagerCount: {
      type: Number,
      default: document.body.clientWidth < 992 ? 5 : 7
    },

    /**
     * 布局方式
     */
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },

    /**
     *是否顯示背景
     */
    background: {
      type: Boolean,
      default: true
    },

    /**
     * 自動滾動
     */
    autoScroll: {
      type: Boolean,
      default: true
    },

    /**
     * 是否隱藏
     */
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    /**
     * 當前頁
     */
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    /**
     * 分頁大小
     */
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      if (this.currentPage * val > this.total) {
        this.currentPage = 1
      }
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>
<style scoped>
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
.pagination-container.hidden {
  display: none;
}
</style>

 

2. 在main.js中我們需要引入,並將該組件注冊為全局組件

查看代碼
// 自定義分頁組件
import Pagination from '@/components/Pagination'

// 全局組件掛載
Vue.component('Pagination', Pagination)

 

3. 具體使用

查看代碼
  <!-- 分頁 -->
    <Pagination
      @pagination="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="currentPage"
      :limit.sync="pageSize"
    />
<!--data中代碼-->
// 分頁信息
      currentPage: 1,
      pageSize: 5,
      total: 0,
  <!-- methods中代碼 -->
  /**
     * 請求分頁
     */
    pagination(p) {
      this.fetchDataNoMessage(p.page, p.limit)
    },

 

思路來源

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM