当前位置:   article > 正文

Vue练习3:组件开发3(页面切换)

Vue练习3:组件开发3(页面切换)

预览

———————————————————————————————————————————

组件文档

Pager组件

 

属性

属性名含义类型必填默认值
current当前页码(总数据量/单页容量)Number1
total总数据量Number0
limit单页容量Number10
visibleNumber可见页码数Number10

事件

事件名含义事件参数参数类型
pageChange($event)页码变化event为当前页码新的页码Number

功能

        首页跳转,单页跳转,末页跳转

        每次必显示可见页码数

        组件第一层未设置样式(pager-container)

———————————————————————————————————————————

注释代码

  1. <template>
  2. <div class="pager-container" v-if="pageNumber > 1"> //只有页面大于1才显示页码
  3. <a @click="handleClick(1)" :class="{ disabled: current === 1}">|&lt;&lt;</a>
  4. <a @click="handleClick(current - 1)" :class="{ disabled: current === 1}">&lt;&lt;</a>
  5. <a @click="handleClick(item)" v-for="(item,i) in numbers" :key="i" :class="{ active: item === current }">{{ item }}</a>
  6. <a @click="handleClick(current + 1)" :class="{ disabled: current === pageNumber }">&gt;&gt;</a>
  7. <a @click="handleClick(pageNumber)" :class="{ disabled: current === pageNumber }">&gt;&gt;|</a>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. props:{ //可传参数
  13. current:{
  14. type:Number,
  15. default: 1,
  16. },
  17. total:{
  18. type:Number,
  19. default: 0,
  20. },
  21. limit:{
  22. type:Number,
  23. default: 10,
  24. },
  25. visibleNumber:{
  26. type:Number,
  27. default: 10,
  28. },
  29. },
  30. computed:{
  31. pageNumber(){
  32. return Math.ceil(this.total / this.limit);
  33. },
  34. visibleMin(){
  35. let min = this.current - Math.floor(this.visibleNumber / 2);
  36. if(min<1){
  37. min = 1;
  38. }
  39. return min;
  40. },
  41. visibleMax(){
  42. let max = this.visibleMin + this.visibleNumber - 1;
  43. if(max > this.pageNumber){
  44. max = this.pageNumber;
  45. }
  46. return max;
  47. },
  48. numbers(){
  49. let nums = [];
  50. let numsLength = this.visibleMin;
  51. if((this.visibleMax - this.visibleMin) < this.visibleNumber - 1){
  52. numsLength = this.visibleMax - this.visibleNumber + 1;
  53. }
  54. for(let i = numsLength; i<= this.visibleMax;i++){
  55. nums.push(i);
  56. }
  57. return nums;
  58. }
  59. },
  60. methods:{
  61. handleClick(newPage){ //页码处理
  62. if(newPage < 1){
  63. newPage = 1;
  64. }
  65. else if( newPage > this.pageNumber){
  66. newPage = this.pageNumber
  67. }
  68. this.$emit("pageChange",newPage) //组件向外传递
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="less" scoped> //样式
  74. @import "~@/styles/var.less"; //引入主题
  75. .pager-container{
  76. display: flex;
  77. justify-content: center;
  78. margin: 20px 0;
  79. cursor: pointer;
  80. a{
  81. color: @danger;
  82. margin:0 6px;
  83. &.disabled {
  84. color: blue;
  85. cursor: not-allowed;
  86. }
  87. &.active{
  88. color: green;
  89. font-weight: bold;
  90. cursor: text;
  91. }
  92. }
  93. }
  94. </style>

———————————————————————————————————————————

可编译代码

  1. <template>
  2. <div class="pager-container" v-if="pageNumber > 1">
  3. <a @click="handleClick(1)" :class="{ disabled: current === 1}">|&lt;&lt;</a>
  4. <a @click="handleClick(current - 1)" :class="{ disabled: current === 1}">&lt;&lt;</a>
  5. <a @click="handleClick(item)" v-for="(item,i) in numbers" :key="i" :class="{ active: item === current }">{{ item }}</a>
  6. <a @click="handleClick(current + 1)" :class="{ disabled: current === pageNumber }">&gt;&gt;</a>
  7. <a @click="handleClick(pageNumber)" :class="{ disabled: current === pageNumber }">&gt;&gt;|</a>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. props:{
  13. current:{
  14. type:Number,
  15. default: 1,
  16. },
  17. total:{
  18. type:Number,
  19. default: 0,
  20. },
  21. limit:{
  22. type:Number,
  23. default: 10,
  24. },
  25. visibleNumber:{
  26. type:Number,
  27. default: 10,
  28. },
  29. },
  30. computed:{
  31. pageNumber(){
  32. return Math.ceil(this.total / this.limit);
  33. },
  34. visibleMin(){
  35. let min = this.current - Math.floor(this.visibleNumber / 2);
  36. if(min<1){
  37. min = 1;
  38. }
  39. return min;
  40. },
  41. visibleMax(){
  42. let max = this.visibleMin + this.visibleNumber - 1;
  43. if(max > this.pageNumber){
  44. max = this.pageNumber;
  45. }
  46. return max;
  47. },
  48. numbers(){
  49. let nums = [];
  50. let numsLength = this.visibleMin;
  51. if((this.visibleMax - this.visibleMin) < this.visibleNumber - 1){
  52. numsLength = this.visibleMax - this.visibleNumber + 1;
  53. }
  54. for(let i = numsLength; i<= this.visibleMax;i++){
  55. nums.push(i);
  56. }
  57. return nums;
  58. }
  59. },
  60. methods:{
  61. handleClick(newPage){
  62. if(newPage < 1){
  63. newPage = 1;
  64. }
  65. else if( newPage > this.pageNumber){
  66. newPage = this.pageNumber
  67. }
  68. this.$emit("pageChange",newPage)
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="less" scoped>
  74. @import "~@/styles/var.less";
  75. .pager-container{
  76. display: flex;
  77. justify-content: center;
  78. margin: 20px 0;
  79. cursor: pointer;
  80. a{
  81. color: @danger;
  82. margin:0 6px;
  83. &.disabled {
  84. color: blue;
  85. cursor: not-allowed;
  86. }
  87. &.active{
  88. color: green;
  89. font-weight: bold;
  90. cursor: text;
  91. }
  92. }
  93. }
  94. </style>

———————————————————————————————————————————

感谢@初琰丶提供的封面

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号