1. 程式人生 > >基於vue2.0實現仿百度前端分頁效果(二)

基於vue2.0實現仿百度前端分頁效果(二)

前言

上篇文章中,已經使用vue實現前端分頁效果,這篇文章我們單獨將分頁抽離出來實現一個分頁元件

先看實現效果圖

程式碼實現

按照慣例,我們在凍手實現的時候還是先想一想vue實現元件的思路

1、需要提前設定哪些引數需要暴露出來給父元件傳遞

<Paging 
      :name="name"
      @change="onPageChange"
      :page-size="size"
      :total="total"
      layout="jumper,total"
      :current-page="curPage"
    />


方法及引數說明
屬性 
page-size 每頁顯示條目個數
total 總條目數
current-page 當前頁數
layout 佈局 預設不顯示 jumper,total

事件
change 當前頁改變時觸發

2、再一個就是涉及到的父子元件通訊

這裡主要通過props向子元件傳遞引數
在子元件中使用emit自定義事件返回資料給父元件

a.字串陣列形式props

props: ['title', 'likes', 'isPublished', 'commentIds', 'author']
或者指定每個prop的值型別
props: {
  title: String,
  likes: Number,
  isPublished: Boolean,
  commentIds: Array,
  author: Object
}

b.props驗證

props: {
    // 基礎的型別檢查 (`null` 匹配任何型別)
    propA: Number,
    // 多個可能的型別
    propB: [String, Number],
    // 必填的字串
    propC: {
      type: String,
      required: true
    },
    // 帶有預設值的數字
    propD: {
      type: Number,
      default: 100
    },
    // 帶有預設值的物件
    propE: {
      type: Object,
      // 物件或陣列預設值必須從一個工廠函式獲取
      default: function () {
        return { message: 'hello' }
      }
    },
    // 自定義驗證函式
    propF: {
      validator: function (value) {
        // 這個值必須匹配下列字串中的一個
        return ['success', 'warning', 'danger'].indexOf(value) !== -1
      }
    }
  }

使用props傳遞資料給子元件 ,子元件主要有三種形式來接收到父元件傳遞過來的引數
props字串陣列、指定每個prop值型別以及props驗證,通常我們會使用props驗證

分析完之後,接下來我們可以凍手實現了

1、這裡我們用vue-cli先建立一個vue專案

安裝vue-cli
$npm install -g vue-cli

建立vue專案
$vue init webpack my-project

專案執行
$cd my-project
$npm run dev

2、在components檔案下建立一個Paging元件

<template>
  <div class="paging clearfix">
     <div class="page-size fl" v-if="isShowTotal">共{{total}}條</div>
       <ul class="page-list fl clearfix">
            <li @click="changePage(currentPage-1)">上一頁</li>
            <li :class="{'active':currentPage==item.val}" v-for="item in pagelist" v-text="item.text" @click="changePage(item.val)">1</li>
            <li @click="changePage(currentPage+1)">下一頁</li>
        </ul>
        <div class="page-jump fl" v-if="isShowJumper">
            前往<input class="input" type="text" v-model="toPage" @keydown="submit(toPage,$event)">頁
            <!-- <button  @click="changePage(toPage)">確定</button> -->
        </div>
  </div>
</template>

<script>
export default {
  name: 'Paging',
  // props:[
  //    'name'
  // ],
  // prop驗證
  props:{
    name:String,
    pageSize: {
      type: Number,
      default: 10
    },
    total: {
      type: Number,
      default: 0
    },
    currentPage: {
      type: Number,
      default: 1
    },
    layout:{
        type: String
    }
  },
  data () {
    return {
            isShowJumper:false,
            isShowTotal:false,
        toPage:'',//跳轉到x頁
            pageGroup:10//可見分頁數量 預設10個分頁數
    }
  },
  created: function () {
        console.log('created');
        this.isShowTotal = this.layout.indexOf('total')!==-1;
        this.isShowJumper = this.layout.indexOf('jumper')!==-1;
    },
    mounted: function () {
        console.log('mounted',this.layout);
    },
    computed:{
        totalPage:function(){
            return Math.ceil(this.total / this.pageSize)
        },
        pagelist:function(){
            var list = [];
            var count = Math.floor(this.pageGroup/2), center = this.currentPage;
            var left = 1,right = this.totalPage;

            if(this.totalPage>this.pageGroup){
                if(this.currentPage>count+1){
                    if(this.currentPage < this.totalPage - count){
                        left = this.currentPage - count;
                        right = this.currentPage + count-1;
                    }else{
                        left = this.totalPage - this.pageGroup+1;
                    }
                }else{
                    right = this.pageGroup;
                }
            }

            // 遍歷新增到數組裡
            while(left<=right){
                list.push({
                    text:left,
                    val:left
                });
                left++;
            }
            return list;
        }
    },
  methods:{
    // 回車事件
    submit(toPage,e){
        // console.log('e.keyCode',toPage,e.keyCode)
        // key.Code === 13表示回車鍵 
        if(e.keyCode === 13){
            //邏輯處理
            this.changePage(toPage);
        }
    },
    changePage:function(idx){
        if(idx!=this.currentPage && idx>0 && idx<=this.totalPage){
            // 觸發父元件事件  pageChange會轉換成小寫pagechange
            this.$emit('change',{curPage:Number(idx)});
            }
    }
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
*{
        padding: 0;
        margin: 0;
}
.fl{
    float: left;
}
.clearfix:after{
    display: block;
    content: '';
    clear: both;
}
.page-size{
        height: 26px;
    line-height: 26px;
}
.page-list{

}
.page-jump{
        height: 26px;
    line-height: 26px;
    margin-left: 20px;
}
.page-jump .input{
    width: 32px;
        padding: 4px 2px;
    border-radius: 2px;
    border: 1px solid #dcdfe6;
    margin: 0 4px;
}
    ul{
        list-style: none;
    }
    ul li{
        float: left;
        color: #606266;
        background: #f4f4f5;
        padding: 2px 8px;
        cursor: pointer;
        border-radius: 2px;
        margin: 0 5px;
    }
    ul>li.active{
        background: #409eff;
        color:#fff;
    }
</style>

3、在父元件中引入並使用元件

<template>
  <div>
    <!-- 分頁元件 -->
    <Paging 
      :name="name"
      @change="onPageChange"
      :page-size="size"
      :total="total"
      layout="jumper,total"
      :current-page="curPage"
    />
  </div>
</template>


<!-- 
Paging屬性 
page-size 每頁顯示條目個數
total 總條目數
current-page 當前頁數
layout 佈局 預設不顯示 jumper,total

Paging事件
change 當前頁改變時觸發
 -->
<script>

import Paging from '@/components/Paging';
export default {
  name: 'Index',
  components:{
    Paging
  },
  data () {
    return {
      msg: 'hello',
      name:'阿健a',
      size:10,
      total:201,
      curPage:1
    }
  },
  methods:{
    onPageChange:function(page){
      this.curPage = page.curPage;
    }
  }
}
</script>

遇到的問題

1、在子元件中修改currentPage時報錯

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders

在使用元件時,傳入的prop,被元件內部又做了一次修改
避免直接修改prop,因為當父元件重新呈現時,值將被覆蓋

changePage:function(idx){
        if(idx!=this.currentPage && idx>0 && idx<=this.totalPage){
            this.currentPage = idx;
            // 觸發父元件事件  pageChange會轉換成小寫pagechange
            this.$emit('change');
            }
    }

解決
修改程式碼,通過emit傳遞curPage給父元件,讓父元件修改

changePage:function(idx){
        if(idx!=this.currentPage && idx>0 && idx<=this.totalPage){
            // 觸發父元件事件  pageChange會轉換成小寫pagechange
            this.$emit('change',{curPage:idx});
            }
    }

父元件監聽事件更新curPage

onPageChange:function(page){
      this.curPage = page.curPage;
    }

最後

以上就是分頁元件的整個實現過程 ,其實只要搞清楚父子元件是如何傳參的,以及我們實現一個元件需要暴露哪些引數給父元件,整個實現過程還是不難的

> 作者:fozero 出處:https://www.cnblogs.com/fozero 本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。

鄭州正規婦科醫院

鄭州哪家不孕不育醫院好

鄭州男科醫院哪家好

鄭州不孕不育醫院