当前位置:首页 > 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

vue实现换页功能

<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>

在父组件中使用:

vue实现换页功能

<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实现用户添加功能

vue实现用户添加功能

实现用户添加功能的基本步骤 在Vue中实现用户添加功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方法: 表单设计与数据绑定 创建用户表单组件,使用v-model实现双向数据绑定:…

vue电影功能实现

vue电影功能实现

Vue电影功能实现 数据获取与展示 使用Axios或Fetch API从电影API获取数据,例如TMDB或豆瓣API。在Vue组件的created或mounted生命周期钩子中发起请求,将返回的电影数…

vue实现收货功能

vue实现收货功能

Vue实现收货功能的基本思路 收货功能通常涉及前端界面交互、后端数据更新和状态管理。在Vue中,可以通过组件化开发实现这一功能,结合API调用与状态管理工具(如Vuex或Pinia)。 创建收货按钮…

vue实现功能插件

vue实现功能插件

Vue 插件实现方法 Vue 插件通常用于封装全局功能(如指令、过滤器、混入等),以下是实现 Vue 插件的典型方式: 插件基本结构 const MyPlugin = { install(…

vue实现tab功能

vue实现tab功能

Vue 实现 Tab 功能的方法 使用动态组件和 v-if 指令 通过 v-if 或 v-show 控制不同 Tab 内容的显示与隐藏。这种方法适合简单的 Tab 切换需求。 <tem…

vue修改功能实现

vue修改功能实现

Vue 修改功能的实现方法 在 Vue 中实现修改功能通常涉及表单绑定、数据更新和事件处理。以下是几种常见的实现方式: 数据双向绑定 使用 v-model 指令实现表单元素与 Vue 实例数据的双向…