使用Vue中mock.js实现分页功能
1.引入Vue.js包
<script src=https://cdn.jsdelivr.net/npm/vue/dist/vue.js></script>
2.引入Mock.js包
<script src="http://mockjs.com/dist/mock.js"></script>
3.Css样式
<style>
.red{
background: #000;
color:white
}
</style>
4.Html 布局
<body>
<div id="app">
<input type="text" v-model="isA">
<p>{{text}}</p>
<p>总页数:{{pageNum}}</p>
<p>当前页:{{currentPage+1}}</p>
<button @click="nextPage">下一页</button>
<button @click="curPage(i)" :class="{red:currentPage==i}" v-for="btn,i in pageNum">
{{btn}}
</button>
<button @click="prePage">上一页</button>
<ul v-for="item in dataShow">
<li>{{item.id}}</li>
<li>{{item.name}}</li>
<li>{{item.ago}}</li>
<li>{{item.sex}}</li>
<li>{{item.job}}</li>
<li>{{item.img}}</li>
</ul>
</div>
</body>
5.Vue.js 代码
<script>
const vm = new Vue({
el:"#app",
data:{
isA:'A',
isB:'B',
text:'',
data:[],
totalPage:[],
pageSize:10,
pageNum:1,
dataShow:"",
currentPage:0
},
computed:{
},
methods:{
nextPage(){
if(this.currentPage === this.pageNum-1) return
this.dataShow = this.totalPage[++this.currentPage];
this.getData()
},
prePage(){
if(this.currentPage === 0) return
this.dataShow = this.totalPage[--this.currentPage];
this.getData()
},
curPage(i){
this.currentPage = i
this.getData()
},
getData(){
this.pageNum = Math.ceil(this.data.length/this.pageSize) || 1;
this.data.map((item,i)=>{
this.totalPage[i] =this.data.slice(this.pageSize * i ,this.pageSize*(i+1))
})
this.dataShow = this.totalPage[this.currentPage];
console.log(this.dataShow )
}
},
mounted(){
var list =Mock.mock({
"userInfo|100":[{
"id|+1":1,
"name":"@name",
"ago|18-28":25,
"sex|1":["男","女"],
"job|1":["web","UI","python","php"] ,
"img":"@image"
}]
})
this.data=list.userInfo
console.log(this.data)
this.getData();
},
})
</script>
6.效果图