一、在components文件夹下新建 pagination.vue

<template>
<div class="page-wrap">
<ul>
<li class="li-page" :plain="true" @click="goPrePage">&laquo;</li>
<li v-for="(i, index) in showPageBtn" :key="index"
:class="{active: i === currentPage, pointer: i, hover: i && i !== currentPage}" @click="pageOffset(i)">
<a v-if="i">{{i}}</a>
<a v-else>···</a>
</li>
<li class="li-page" :plain="true" @click="goNextPage">&raquo;</li>
</ul>
</div>
</template> <script>
export default {
name: "pagination",
props: {
num: Number,
limit: Number
},
data: () => ({
offset: 0
}),
computed: {
prePage() {
return this.offset !== 0 && this.num;
},
nextPage() {
return (this.offset + this.limit < this.num) && this.num
},
totalPage() {
return Math.ceil(this.num / this.limit)
},
currentPage() {
return Math.ceil(this.offset / this.limit) + 1
},
showPageBtn() {
const pageNum = this.totalPage;
const index = this.currentPage;
if (pageNum <= 5) return [...new Array(pageNum)].map((v, i) => i + 1);
if (index <= 2) return [1, 2, 3, 0, pageNum];
if (index >= pageNum - 1) return [1, 0, pageNum - 2, pageNum - 1, pageNum];
if (index === 3) return [1, 2, 3, 4, 0, pageNum];
if (index === pageNum - 2) return [1, 0, pageNum - 3, pageNum - 2, pageNum - 1, pageNum];
return [1, 0, index - 1, index, index + 1, 0, pageNum]
}
},
methods: {
pageOffset(i) {
if (i === 0 || i === this.currentPage) return;
this.offset = (i - 1) * this.limit;
this.$emit('getNew', this.offset);
},
goPrePage() {
if (!this.prePage) {
// alert("这里是第一页呀,亲!");
this.$message({
showClose: true,
message: '这是第一页哦!',
type: 'warning'
});
return;
}
this.offset -= this.limit;
this.$emit('getNew', this.offset);
},
goNextPage() {
if (!this.nextPage) {
// alert("已经是最后一页了呢~");
this.$message({
showClose: true,
message: '这是最后一页啦!',
type: 'warning'
});
return;
}
this.offset += this.limit;
this.$emit('getNew', this.offset);
}
}
} </script> <style scoped>
.li-page {
/* line-height: 25px; */
cursor: pointer;
color: #505362;
background-color: #f4f4f5;
} .active {
border-color: #0C9F9A;
background-color: #0C9F9A;
} .pointer {
cursor: pointer;
} .hover {
color: #FFF;
background-color: #FFF;
} .active a {
color: #FFF;
} .page-wrap {
font-size: 14px;
margin-top: 10px;
text-align: center;
} .page-wrap ul {
display: inline-block;
list-style: none;
overflow: hidden;
padding-inline-start: 0px;
} .page-wrap li {
float: left;
color: #505362;
padding: 10px 17px;
margin: 0 5px;
border-radius: 3px;
user-select: none;
border: 1px solid transparent;
} </style>

二、在需要分页的页面引入组件

<script>
import pagination from "../../components/pagination.vue"
export default {
components: {
pagination
},
data(){
return {
numpage: 0,
limit: 10,
currentList: [],
list:[{
name:'dsa',id:1
},{
name:'dsa',id:2
},{
name:'dsa',id:3
},]
}
}, mounted() {
this.getNew(0);
this.numpage = this.list.length;
},
methods:{
// 分页
getNew(value) {
this.currentList = this.list.slice(value, value + this.limit);
}, }
}
</script>

三、将组件引入到页面使用

<div>
<!-- 分页 -->
<pagination :num="numpage" :limit="limit" @getNew="getNew"></pagination>
</div>

好啦,一个分页组件就这么封装好啦!

/********
*
* .-~~~~~~~~~-._ _.-~~~~~~~~~-.
* __.' 欢迎访问 ~. .~ `.__
* .'// 我的博客 \./ ☞ 送你小 ☜ \\`.
* .'// | \\`.
* .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
* .'//.-" `-. | .-' "-.\\`.
* .'//______.============-.. \ | / ..-============.______\\`.
* .'______________________________\|/______________________________`.
*/

vue 单独封装分页组件的更多相关文章

  1. 基于Vue封装分页组件

    使用Vue做双向绑定的时候,可能经常会用到分页功能 接下来我们来封装一个分页组件 先定义样式文件 pagination.css ul, li { margin: 0px; padding: 0px;} ...

  2. vue 封装分页组件

    分页 一般都是调接口, 接口为这种格式 {code: 0, msg: "success",…} code:0 data:{ content:[{content: "11& ...

  3. vue封装分页组件

    element提供的分页是已经封装好的组件,在这里再次封装是为了避免每个用到分页的页面点击跳转时都要写一遍跳转请求 分页组件 <!--分页组件--> <template> &l ...

  4. Vue 实现一个分页组件

    实现分页组件要分三个部分 样式,逻辑,和引用 首先新建一个vue文件用来承载组件内容 第一步:构建样式 <template> <nav> <ul class=" ...

  5. vue中使用分页组件、将从数据库中查询出来的数据分页展示(前后端分离SpringBoot+Vue)

    文章目录 1.看实现的效果 2.前端vue页面核心代码 2.1. 表格代码(表格样式可以去elementui组件库直接调用相应的) 2.2.分页组件代码 2.3 .script中的代码 3.后端核心代 ...

  6. Vue自行封装常用组件-弹出框

    使用方法: 1.在父组件中引入"box.vue" //import popUpBox from "./box.vue";   2.在父组件中注册 popUpBo ...

  7. Vue 数组封装和组件data定义为函数一些猜测

     数组封装 var vm={ list:[0,1] } var push=vm.list.push;//把数组原来的方法存起来 vm.list.push=function(arg){//重新定义数组的 ...

  8. Vue文件封装日历组件

    封装就是要具有灵活性,样式自适应,调用的时候传入props就可以变成自己想要的样式. 效果展示网址:https://1963331542.github.io/ 源代码: <template> ...

  9. Vue中封装axios组件实例

    首先要创建一个网络模块network文件夹  里面要写封装好的几个组件 在config.js里面这样写 在index.js要这样写 core.js文件里面内容如下 然后要在main.js文件里面要设置 ...

  10. [ vue ] Quasar封装q-dialog组件,在外层实现弹出框的开启和关闭

    场景描述: 见:https://www.cnblogs.com/remly/p/12981582.html 具体实现: <!-- 父组件 --> <template> < ...

随机推荐

  1. POJ2955 Brackets (区间DP)

    很好的区间DP题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+ ...

  2. bootstrapValidator 参数校验框架

    bootstrap:能够增加兼容性的强大框架. 因为项目需要数据验证,看bootstrapValidator 还不错,就上手一直,完美兼容,话不多说. 需要引用css: bootstrap.min.c ...

  3. ByPass

    WebShell-ByPass php一句话木马 <?php eval($_REQUEST['a']]);?> 拦截进行替换 替换eval() assert() 替换$_REQUEST[' ...

  4. Linux基础_5_文件管理

    创建 touch 文件名 #创建文件 stat 文件名 #查看文件属性 touch -am 文件名 #更改文件的访问时间及修改时间 删除 rm -rf s/* #递归强制删除s目录下的所有内容(包括文 ...

  5. Pipeline流水线设计的最佳实践

    谈到到DevOps,持续交付流水线是绕不开的一个话题,相对于其他实践,通过流水线来实现快速高质量的交付价值是相对能快速见效的,特别对于开发测试人员,能够获得实实在在的收益.很多文章介绍流水线,不管是j ...

  6. CF Round #829 题解 (Div. 2)

    F 没看所以摆了 . 看拜月教教主 LHQ 在群里代打恰钱 /bx 目录 A. Technical Support (*800) B. Kevin and Permutation (*800) C. ...

  7. 《上传那些事儿之Nest与Koa》——文件格式怎么了!

    转载自:juejin - 马睿不想说话 概要 本文主要针对在使用node作为服务端接口时,前端上传上传文件至node作为中转,再次上传至oss/cdn的场景.以及针对在这个过程中,需要对同一个文件进行 ...

  8. Arctic 基于 Hive 的流批一体实践

    背景 随着大数据业务的发展,基于 Hive 的数仓体系逐渐难以满足日益增长的业务需求,一方面已有很大体量的用户,但是在实时性,功能性上严重缺失:另一方面 Hudi,Iceberg 这类系统在事务性,快 ...

  9. vue-axios删除操作

    <template> <div class="nav"> <input v-model="location" type=" ...

  10. python的一些运算符

    # 1.算术运算符 print('1.算术运算符') # 1.1 + 求和 a = 10 b = 20 c = a + b print(c) print('a+b={}'.format(c)) pri ...