您好,登錄后才能下訂單哦!
這篇文章主要介紹了基于vue2.0如何實(shí)現(xiàn)仿百度前端分頁(yè)效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
先看實(shí)現(xiàn)效果圖
代碼實(shí)現(xiàn)
按照慣例,我們?cè)趦鍪謱?shí)現(xiàn)的時(shí)候還是先想一想vue實(shí)現(xiàn)組件的思路
1、需要提前設(shè)定哪些參數(shù)需要暴露出來(lái)給父組件傳遞
<Paging :name="name" @change="onPageChange" :page-size="size" :total="total" layout="jumper,total" :current-page="curPage" />
方法及參數(shù)說(shuō)明
屬性
page-size 每頁(yè)顯示條目個(gè)數(shù)
total 總條目數(shù)
current-page 當(dāng)前頁(yè)數(shù)
layout 布局 默認(rèn)不顯示 jumper,total
事件
change 當(dāng)前頁(yè)改變時(shí)觸發(fā)
2、再一個(gè)就是涉及到的父子組件通信
這里主要通過(guò)props向子組件傳遞參數(shù)
在子組件中使用emit自定義事件返回?cái)?shù)據(jù)給父組件
a.字符串?dāng)?shù)組形式props
props: ['title', 'likes', 'isPublished', 'commentIds', 'author']
或者指定每個(gè)prop的值類型
props: { title: String, likes: Number, isPublished: Boolean, commentIds: Array, author: Object }
b.props驗(yàn)證
props: { // 基礎(chǔ)的類型檢查 (`null` 匹配任何類型) propA: Number, // 多個(gè)可能的類型 propB: [String, Number], // 必填的字符串 propC: { type: String, required: true }, // 帶有默認(rèn)值的數(shù)字 propD: { type: Number, default: 100 }, // 帶有默認(rèn)值的對(duì)象 propE: { type: Object, // 對(duì)象或數(shù)組默認(rèn)值必須從一個(gè)工廠函數(shù)獲取 default: function () { return { message: 'hello' } } }, // 自定義驗(yàn)證函數(shù) propF: { validator: function (value) { // 這個(gè)值必須匹配下列字符串中的一個(gè) return ['success', 'warning', 'danger'].indexOf(value) !== -1 } } }
使用props傳遞數(shù)據(jù)給子組件 ,子組件主要有三種形式來(lái)接收到父組件傳遞過(guò)來(lái)的參數(shù)
props字符串?dāng)?shù)組、指定每個(gè)prop值類型以及props驗(yàn)證,通常我們會(huì)使用props驗(yàn)證
分析完之后,接下來(lái)我們可以凍手實(shí)現(xiàn)了
1、這里我們用vue-cli先創(chuàng)建一個(gè)vue項(xiàng)目
安裝vue-cli
$npm install -g vue-cli
創(chuàng)建vue項(xiàng)目
$vue init webpack my-project
項(xiàng)目運(yùn)行
$cd my-project $npm run dev
2、在components文件下創(chuàng)建一個(gè)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)">上一頁(yè)</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)">下一頁(yè)</li> </ul> <div class="page-jump fl" v-if="isShowJumper"> 前往<input class="input" type="text" v-model="toPage" @keydown="submit(toPage,$event)">頁(yè) <!-- <button @click="changePage(toPage)">確定</button> --> </div> </div> </template> <script> export default { name: 'Paging', // props:[ // 'name' // ], // prop驗(yàn)證 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:'',//跳轉(zhuǎn)到x頁(yè) pageGroup:10//可見(jiàn)分頁(yè)數(shù)量 默認(rèn)10個(gè)分頁(yè)數(shù) } }, 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; } } // 遍歷添加到數(shù)組里 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){ // 觸發(fā)父組件事件 pageChange會(huì)轉(zhuǎn)換成小寫(xiě)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> <!-- 分頁(yè)組件 --> <Paging :name="name" @change="onPageChange" :page-size="size" :total="total" layout="jumper,total" :current-page="curPage" /> </div> </template>
<!-- Paging屬性 page-size 每頁(yè)顯示條目個(gè)數(shù) total 總條目數(shù) current-page 當(dāng)前頁(yè)數(shù) layout 布局 默認(rèn)不顯示 jumper,total Paging事件 change 當(dāng)前頁(yè)改變時(shí)觸發(fā) --> <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>
遇到的問(wèn)題
1、在子組件中修改currentPage時(shí)報(bào)錯(cuò)
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders
在使用組件時(shí),傳入的prop,被組件內(nèi)部又做了一次修改
避免直接修改prop,因?yàn)楫?dāng)父組件重新呈現(xiàn)時(shí),值將被覆蓋
changePage:function(idx){ if(idx!=this.currentPage && idx>0 && idx<=this.totalPage){ this.currentPage = idx; // 觸發(fā)父組件事件 pageChange會(huì)轉(zhuǎn)換成小寫(xiě)pagechange this.$emit('change'); } }
解決
修改代碼,通過(guò)emit傳遞curPage給父組件,讓父組件修改
changePage:function(idx){ if(idx!=this.currentPage && idx>0 && idx<=this.totalPage){ // 觸發(fā)父組件事件 pageChange會(huì)轉(zhuǎn)換成小寫(xiě)pagechange this.$emit('change',{curPage:idx}); } }
父組件監(jiān)聽(tīng)事件更新curPage
onPageChange:function(page){ this.curPage = page.curPage; }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“基于vue2.0如何實(shí)現(xiàn)仿百度前端分頁(yè)效果”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。