当前位置:首页 > VUE

vue实现换页功能

2026-01-15 08:15:29VUE

Vue 实现分页功能

使用第三方库(如 Element UI)

安装 Element UI:

npm install element-ui

main.js 中引入:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

在组件中使用 el-pagination

<template>
  <div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      // 加载对应页面的数据
      this.fetchData();
    },
    fetchData() {
      // 根据 currentPage 和 pageSize 获取数据
      console.log(`加载第 ${this.currentPage} 页数据`);
    }
  }
};
</script>

自定义分页组件

创建一个自定义分页组件 Pagination.vue

<template>
  <div class="pagination">
    <button 
      @click="changePage(currentPage - 1)" 
      :disabled="currentPage === 1">
      上一页
    </button>
    <span v-for="page in pages" :key="page">
      <button 
        @click="changePage(page)" 
        :class="{ active: currentPage === page }">
        {{ page }}
      </button>
    </span>
    <button 
      @click="changePage(currentPage + 1)" 
      :disabled="currentPage === totalPages">
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: {
      type: Number,
      required: true
    },
    itemsPerPage: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage);
    },
    pages() {
      const range = [];
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i);
      }
      return range;
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page);
      }
    }
  }
};
</script>

<style>
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

在父组件中使用:

<template>
  <div>
    <!-- 显示数据 -->
    <Pagination
      :total-items="totalItems"
      :items-per-page="itemsPerPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

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

export default {
  components: { Pagination },
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 10,
      totalItems: 100
    };
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    fetchData() {
      console.log(`加载第 ${this.currentPage} 页数据`);
    }
  }
};
</script>

结合后端 API 实现分页

methods 中调用 API:

methods: {
  async fetchData() {
    try {
      const response = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          limit: this.pageSize
        }
      });
      this.dataList = response.data.items;
      this.total = response.data.total;
    } catch (error) {
      console.error('获取数据失败:', error);
    }
  }
}

分页样式优化

添加 CSS 样式:

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}

.pagination button:hover:not(:disabled) {
  background: #f5f5f5;
}

.pagination button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.pagination button.active {
  background: #42b983;
  color: white;
  border-color: #42b983;
}

vue实现换页功能

标签: 功能换页
分享给朋友:

相关文章

h5能实现的功能

h5能实现的功能

H5能实现的功能 H5(HTML5)是一种现代的网页技术标准,具备丰富的功能和特性,适用于多种场景。以下是H5能够实现的主要功能: 多媒体支持 H5原生支持音频和视频播放,无需依赖Flash等插件…

vue实现后台功能

vue实现后台功能

Vue 实现后台功能的方法 Vue.js 是一个流行的前端框架,适合构建后台管理系统。以下是实现后台功能的常见方法和步骤。 项目初始化 使用 Vue CLI 或 Vite 初始化项目: # V…

vue实现拨号功能

vue实现拨号功能

Vue 实现拨号功能 使用 tel: 协议实现基础拨号 在 Vue 中可以通过 HTML 的 <a> 标签结合 tel: 协议实现拨号功能。这种方式适用于移动端浏览器,点击后会直接调用系统…

vue怎么实现功能

vue怎么实现功能

Vue 是一个渐进式 JavaScript 框架,常用于构建用户界面。以下是一些常见的功能实现方法: 数据绑定 Vue 通过 v-model 实现双向数据绑定,适用于表单输入等场景: <te…

php实现评论功能

php实现评论功能

数据库设计 创建评论表(示例结构): CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id`…

php购物车功能实现

php购物车功能实现

数据库设计 购物车功能通常需要设计数据库表来存储商品和用户信息。常见的表包括: products 表:存储商品信息,如商品ID、名称、价格、库存等。 users 表:存储用户信息,如用户ID、用户…