首页 > web前端 > Vue.js > 正文

VUE3开发入门教程:使用组件实现分页

WBOY
发布: 2023-06-16 08:48:06
原创
2455人浏览过

vue3开发入门教程:使用组件实现分页

分页是一个常见的需求,因为在实际开发中,我们往往需要将大量的数据分成若干页以展示给用户。在VUE3开发中,可以通过使用组件实现分页功能,本文将介绍如何使用组件实现简单的分页功能。

1.创建组件

首先,我们需要创建一个分页组件,使用“vue create”命令创建VUE项目,并在src/components目录下创建Pagination.vue文件。

在Pagination.vue文件中,我们需要声明组件的名称、data数据项、methods方法等。代码如下:

立即学习前端免费学习笔记(深入)”;

<template>
  <div>
    <ul v-if="pages.length">
      <li v-if="currentPage !== 1" @click="changePage(currentPage - 1)"><</li>
      <li v-for="page in pages" :key="page" :class="{'active': page === currentPage}" @click="changePage(page)">{{ page }}</li>
      <li v-if="currentPage !== totalPage && totalPage > 1" @click="changePage(currentPage + 1)">></li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalPage: 0,
      pages: []
    }
  },
  props: {
    total: {
      type: Number,
      default: 0
    },
    pageSize: {
      type: Number,
      default: 10
    }
  },
  watch: {
    total() {
      this.init()
    }
  },
  methods: {
    init() {
      this.totalPage = Math.ceil(this.total / this.pageSize)
      this.pages = []
      for (let i = 1; i <= this.totalPage; i++) {
        this.pages.push(i)
      }
    },
    changePage(page) {
      if (this.currentPage === page) return
      this.currentPage = page
      // 触发自定义事件
      this.$emit('page-change', this.currentPage)
    }
  },
  created() {
    this.init()
  }
}
</script>

<style>
.pagination {
  text-align: center;
}

ul {
  display: inline-block;
  padding-left: 0;
  margin: 0;
  border-radius: 4px;
}

li {
  display: inline;
  color: #2b2b2b;
  float: left;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border: 1px solid #eee;
  margin-right: 3px;
  cursor: pointer;
}

li:hover {
  background-color: #eee;
}

.active {
  background-color: #00a0e9;
  color: #fff;
  border: 1px solid #00a0e9;
  cursor: default;
}

li:first-child {
  margin-right: 10px !important;
}

li:last-child {
  margin-left: 10px !important;
}
</style>
登录后复制

在代码中,我们需要实现分页的初始化方法init,以及向外提供分页切换的方法changePage。同时,我们通过watch监听total属性的变化,当total属性变化时,重新调用init方法初始化分页。

2.引入组件

86CMS企业网站系统中英繁三语版1.2
86CMS企业网站系统中英繁三语版1.2

86CMS企业网站系统为智能ASP网站管理程序,适合中小企业自建网站、二次开发使用。本程序具有体积小巧、程序文件结构严谨、界面清爽简单、功能强大、非专业人士使用入门快、中小企业使用投资小等实用特点。本版本为中英繁版本。86CMS企业网站系统中英繁三语版 v1.2 更新1.修正英文版的flash幻灯调用出错问题。2.修正英文版导航菜单设置出错问题。3.增加信息是否显示在中英各版的功能。4.调整首页视

86CMS企业网站系统中英繁三语版1.2 1
查看详情 86CMS企业网站系统中英繁三语版1.2

接下来,在我们的组件中使用分页组件。例如,我们在App.vue中引入Pagination组件,并使用v-for指令在模板中循环展示数据,同时监听Pagination组件的page-change自定义事件。

代码如下:

<template>
  <div class="container">
    <ul>
      <li v-for="item in list" :key="item.id">{{item.text}}</li>
    </ul>
    <Pagination :total="total" :page-size="pageSize" @page-change="handlePageChange"/>
  </div>
</template>

<script>
import Pagination from './components/Pagination'

export default {
  name: 'App',
  components: {
    Pagination
  },
  data() {
    return {
      total: 100,
      list: []
    }
  },
  methods: {
    initData() {
      for (let i = 1; i <= this.total; i++) {
        this.list.push({
          id: i,
          text: `第 ${i} 条数据`
        })
      }
    },
    handlePageChange(page) {
      const start = (page - 1) * this.pageSize
      const end = start + this.pageSize > this.total ? this.total : start + this.pageSize
      this.list = []
      for (let i = start + 1; i <= end; i++) {
        this.list.push({
          id: i,
          text: `第 ${i} 条数据`
        })
      }
    }
  },
  created() {
    this.initData()
  }
}
</script>

<style>
.container {
  margin: 0 auto;
  width: 500px;
}

ul {
  padding: 0;
  list-style: none;
}

li {
  border: 1px solid #eee;
  margin-top: 10px;
  padding: 10px;
}
</style>
登录后复制

在代码中,我们需要通过计算得到当前页展示的数据范围,并根据范围重新渲染数据列表。

3.运行并测试

最后,在命令行中运行“npm run serve”命令,启动开发服务器,并在浏览器中访问http://localhost:8080/,即可看到页面展示了分页组件和数据列表。你可以尝试在页面上点击分页按钮,观察数据列表是否能够正确地切换页码。

以上就是使用组件实现分页的简单示例,在实际开发中,你可以根据项目的需求对分页组件的样式和属性进行自定义,进一步提升用户体验。

以上就是VUE3开发入门教程:使用组件实现分页的详细内容,更多请关注php中文网其它相关文章!

相关标签:
vue
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号