首页 web前端 Vue.js Vue组件实战:分页组件开发

Vue组件实战:分页组件开发

Nov 24, 2023 am 08:56 AM
vue 组件 分页

Vue组件实战:分页组件开发

Vue组件实战:分页组件开发

介绍

在Web应用程序中,分页功能是必不可少的一个组件。一个好的分页组件应该展示简洁明了,功能丰富,而且易于集成和使用。

在本文中,我们将介绍如何使用Vue.js框架来开发一个高度可定制化的分页组件。我们将通过代码示例来详细说明如何使用Vue组件开发。

技术栈

  • Vue.js 2.x
  • JavaScript (ES6)
  • HTML5和CSS3

开发环境

  • Node.js v8.9.3
  • npm v5.5.1
  • Vue.js v2.5.2

分页组件需求

  • 通过props接收总页面数(total)和当前页面数(current)属性
  • 可以配置显示的最大页码数(maxShown)
  • 可以配置按钮显示的文本 (prevText和nextText) 和按钮样式
  • 点击页码可以切换到相应的页面
  • 当前页码高亮显示
  • 当前页面没有前一页时,忽略上一页按钮的点击事件
  • 当前页面没有后一页时,忽略下一页按钮的点击事件

设计思路和代码实现

根据需求,我们将分页组件拆分为多个小组件来实现。我们需要创建以下3个小组件:

  1. Pagination.vue

主分页组件,负责分页数据和逻辑的处理。向子组件传递分页信息和响应子组件的事件。

  1. Button.vue

该组件为按钮组件,用于创建分页按钮。

  1. Page.vue

该组件用于创建单个页面块,包含页面标号和状态。页面块可以是当前页面或非当前页面。

接下来,让我们使用代码来实现以上3个组件。

  1. Pagination.vue
<template>
  <div class="pagination-container">
    <button-prev :current="current" @onPrev="prev"></button-prev>
    <page v-for="page in pages"
      :key="page"
      :page="page"
      :is-selected="page === current"
      @on-page-selected="selectPage"></page>
    <button-next :current="current" :total="total" @onNext="next"></button-next>
  </div>
</template>
<script>
import ButtonPrev from './ButtonPrev.vue';
import ButtonNext from './ButtonNext.vue';
import Page from './Page.vue';

export default {
  components: { ButtonPrev, ButtonNext, Page },
  props: {
    total: {
      type: Number,
      default: 10
    },
    current: {
      type: Number,
      default: 1
    },
    maxShown: {
      type: Number,
      default: 5
    },
    prevText: {
      type: String,
      default: '上一页'
    },
    nextText: {
      type: String,
      default: '下一页'
    }
  },
  computed: {
    pages () {
      const start = Math.max(1, this.current - Math.floor(this.maxShown / 2));
      const end = Math.min(this.total, start + this.maxShown - 1);
      return Array.from({ length: end - start + 1 }, (v, k) => start + k);
    }
  },
  methods: {
    selectPage (page) {
      if (this.current === page) return;
      this.current = page;
      this.$emit('onPageChanged', page);
    },
    prev () {
      if (this.current > 1) {
        this.selectPage(this.current - 1);
      }
    },
    next () {
      if (this.current < this.total) {
        this.selectPage(this.current + 1);
      }
    }
  }
}
</script>

上面的代码中,我们首先import了ButtonPrev、ButtonNext和Page组件。接着,用props方式获取了total, current, maxShown, prevText和nextText属性,并定义了计算属性pages,根据当前页码(current)和最大页码数(maxShown)得到一个包含页码数的数组,以在组件中呈现。

我们还定义了selectPage方法,在该方法中,如果页码(page)与当前页码(current)相同,则返回或不做任何事情。否则,将新页码发出给父组件。

prev()和next()方法用于处理上一页和下一页事件,并防止event被响应。

  1. ButtonPrev.vue
<template>
    <button
      class="btn-previous"
      :disabled="current === 1"
      @click="onPrev()">
      {{ prevText }}
    </button>
</template>

<script>
export default {
  props: {
    prevText: {
      type: String,
      default: '上一页'
    },
    current: {
      type: Number,
      default: 1
    }
  },
  methods: {
    onPrev () {
      this.$emit('onPrev');
    }
  }
}
</script>

<style scoped>
.btn-previous {
  border: none;
  color: #333;
  display: inline-block;
  font-size: 16px;
  padding: 6px 12px;
  margin-right: 5px;
  background-color:#fff;
  cursor: pointer;
  border-radius: 2px;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
}
.btn-previous:disabled {
  color: #ccc;
  cursor: default;
}
</style>

上述代码中,我们首先通过props获取了当前页码(current)和上一页按钮的文本(prevText)属性。在模版中,使用类绑定(disabled)控制按钮使用状态。定义了一个onPrev方法,该方法触发父组件的onPrev事件。

  1. ButtonNext.vue
<template>
    <button
      class="btn-next"
      :disabled="current === total"
      @click="onNext()">
      {{ nextText }}
    </button>
</template>

<script>
export default {
  props: {
    total: {
      type: Number,
      default: 10
    },
    nextText: {
      type: String,
      default: '下一页'
    },
    current: {
      type: Number,
      default: 1
    }
  },
  methods: {
    onNext () {
      this.$emit('onNext');
    }
  }
}
</script>

<style scoped>
.btn-next {
  border: none;
  color: #333;
  display: inline-block;
  font-size: 16px;
  padding: 6px 12px;
  margin-left: 5px;
  background-color: #fff;
  cursor: pointer;
  border-radius: 2px;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
}
.btn-next:disabled {
  color: #ccc;
  cursor: default;
}
</style>

上述代码中,我们将ButtonPrev.vue的代码复制了一份,稍微改了一下文本和判断条件。

  1. Page.vue
<template>
  <button :class="{ current: isSelected }" class="btn-page" @click="onPageSelected(page)">
    {{ page }}
  </button>
</template>

<script>
export default {
  props: {
    page: {
      type: Number,
      required: true
    },
    isSelected: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    onPageSelected () {
      this.$emit('onPageSelected', this.page);
    }
  }
}
</script>

<style scoped>
.btn-page {
  border: none;
  color: #333;
  display: inline-block;
  font-size: 16px;
  padding: 6px 12px;
  margin-left: 5px;
  background-color: #fff;
  cursor: pointer;
  border-radius: 2px;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
}
.btn-page.current {
  background-color: #0078d7;
  color: #fff;
}
</style>

上述代码中,我们通过props获取了该页码的值(page)和按钮的isSelected属性。在模板中,使用类绑定("current")高亮显示选中的页面。

我们还定义了一个onPageSelected方法,该方法会触发父组件的onPageSelected事件。

最后,这些组件可以在任何Vue.js应用程序中的template中使用,如下所示:

<template>
  <div>
    <pagination
      :total="total"
      :current="current"
      :maxShown="maxShown"
      :prevText="prevText"
      :nextText="nextText"
      @onPageChanged="onPageChanged"></pagination>
    <ul>
      <li v-for="(item, index) in items" :key="index">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
import Pagination from './Pagination.vue';

export default {
  components: {
    Pagination
  },
  data () {
    return {
      current: 1,
      maxShown: 10,
      prevText: '上一页',
      nextText: '下一页',
      total: 10,
      pageSize: 10,
      items: [{ name: 'Item 1' }, { name: 'Item 2' }, { name: 'Item 3' }]
    }
  },
  methods: {
    onPageChanged (page) {
      console.log('Page changed to: ', page);
      // 当前页面数据请求
    }
  }
}
</script>

上述代码中,我们引入了Pagination组件,并将其作为template中的父组件。我们还将total, current和maxShown绑定到组件,以便获取到它们的值。在onPageChanged方法中,我们可以处理页面更改事件,并根据当前页码请求相应的数据。

以上是Vue组件实战:分页组件开发的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Laravel 教程
1605
29
PHP教程
1510
276
Vue的反应性转换(实验,然后被删除)的意义是什么? Vue的反应性转换(实验,然后被删除)的意义是什么? Jun 20, 2025 am 01:01 AM

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

VUE中的服务器端渲染SSR是什么? VUE中的服务器端渲染SSR是什么? Jun 25, 2025 am 12:49 AM

Server-Serdendering(SSR)InvueImProvesperformandSeobyGeneratingHtmlonTheserver.1.TheserverrunsvueApcodeAmpCodeAndGeneratesHtmlbBasedonThecurrentRoute.2.thathtmlssenttothebrowserimmed.3.vuehirative eveirtive eveirtive eveirtive eveirtive eveirtive eveirtive eveirtive eveirtiveThepage evepage evepage

在MySQL中以极限和偏移的限制结果 在MySQL中以极限和偏移的限制结果 Jul 05, 2025 am 02:41 AM

MySQL分页常用LIMIT和OFFSET实现,但大数据量下性能较差。1.LIMIT控制每页数量,OFFSET控制起始位置,语法为LIMITNOFFSETM;2.性能问题源于OFFSET扫描过多记录并丢弃,导致效率低;3.优化建议包括使用游标分页、索引加速、懒加载;4.游标分页通过上一页最后一条记录的唯一值定位下一页起点,避免OFFSET,适合“下一页”操作,不适合随机跳转。

如何使用VUE构建组件库? 如何使用VUE构建组件库? Jul 10, 2025 pm 12:14 PM

搭建Vue组件库需围绕业务场景设计结构,并遵循开发、测试、发布的完整流程。1.结构设计应按功能模块分类,包括基础组件、布局组件和业务组件;2.使用SCSS或CSS变量统一主题与样式;3.统一命名规范并引入ESLint和Prettier保证代码风格一致;4.配套文档站点展示组件用法;5.使用Vite等工具打包为NPM包并配置rollupOptions;6.发布时遵循semver规范管理版本与changelog。

如何在VUE中实现过渡和动画? 如何在VUE中实现过渡和动画? Jun 24, 2025 pm 02:17 PM

ToaddtransitionsandanimationsinVue,usebuilt-incomponentslikeand,applyCSSclasses,leveragetransitionhooksforcontrol,andoptimizeperformance.1.WrapelementswithandapplyCSStransitionclasseslikev-enter-activeforbasicfadeorslideeffects.2.Useforanimatingdynam

如何用PHP开发问答社区平台 PHP互动社区变现模式详解 如何用PHP开发问答社区平台 PHP互动社区变现模式详解 Jul 23, 2025 pm 07:21 PM

1.PHP开发问答社区首选Laravel MySQL Vue/React组合,因生态成熟、开发效率高;2.高性能需依赖缓存(Redis)、数据库优化、CDN和异步队列;3.安全性必须做好输入过滤、CSRF防护、HTTPS、密码加密及权限控制;4.变现可选广告、会员订阅、打赏、佣金、知识付费等模式,核心是匹配社区调性和用户需求。

如何用PHP开发AI智能表单系统 PHP智能表单设计与分析 如何用PHP开发AI智能表单系统 PHP智能表单设计与分析 Jul 25, 2025 pm 05:54 PM

选择合适的PHP框架需根据项目需求综合考虑:Laravel适合快速开发,提供EloquentORM和Blade模板引擎,便于数据库操作和动态表单渲染;Symfony更灵活,适合复杂系统;CodeIgniter轻量,适用于对性能要求较高的简单应用。2.确保AI模型准确性需从高质量数据训练、合理选择评估指标(如准确率、召回率、F1值)、定期性能评估与模型调优入手,并通过单元测试和集成测试保障代码质量,同时持续监控输入数据以防止数据漂移。3.保护用户隐私需采取多项措施:对敏感数据进行加密存储(如AES

Vue成品资源网站免费入口 完整Vue成品永久在线观看 Vue成品资源网站免费入口 完整Vue成品永久在线观看 Jul 23, 2025 pm 12:39 PM

本文为Vue开发者和学习者精选了一系列顶级的成品资源网站。通过这些平台,你可以免费在线浏览、学习甚至复用海量高质量的Vue完整项目,从而快速提升开发技能和项目实践能力。

See all articles