当前位置:首页 > VUE

用vue实现加载分页

2026-02-25 00:14:25VUE

使用Vue实现分页加载

安装依赖

确保项目中已安装axios用于数据请求,若未安装可通过以下命令安装:

npm install axios

基本分页组件结构

在Vue组件中定义分页所需的数据和方法:

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>当前页: {{ currentPage }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      items: [],
      currentPage: 1,
      totalPages: 1,
      perPage: 10
    };
  },
  methods: {
    fetchData() {
      axios.get(`/api/items?page=${this.currentPage}&per_page=${this.perPage}`)
        .then(response => {
          this.items = response.data.data;
          this.totalPages = response.data.total_pages;
        });
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
        this.fetchData();
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
        this.fetchData();
      }
    }
  },
  created() {
    this.fetchData();
  }
};
</script>

后端API示例

假设后端使用类似RESTful的接口,返回格式如下:

{
  "data": [...],
  "total_pages": 5
}

优化加载体验

添加加载状态提示,避免用户重复点击:

data() {
  return {
    isLoading: false
  };
},
methods: {
  fetchData() {
    this.isLoading = true;
    axios.get(...)
      .then(...)
      .finally(() => {
        this.isLoading = false;
      });
  }
}

分页样式优化

使用CSS美化分页按钮,例如:

.pagination {
  margin-top: 20px;
}
.pagination button {
  padding: 5px 10px;
  margin: 0 5px;
}
.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

动态页码显示

扩展分页组件,显示更多页码选项:

用vue实现加载分页

<template>
  <div class="pagination">
    <button @click="goToPage(1)" :disabled="currentPage === 1">首页</button>
    <button v-for="page in visiblePages" 
            @click="goToPage(page)"
            :class="{ active: currentPage === page }">
      {{ page }}
    </button>
    <button @click="goToPage(totalPages)" :disabled="currentPage === totalPages">末页</button>
  </div>
</template>

computed: {
  visiblePages() {
    const range = 2; // 显示当前页前后各2页
    let start = Math.max(1, this.currentPage - range);
    let end = Math.min(this.totalPages, this.currentPage + range);
    return Array.from({ length: end - start + 1 }, (_, i) => start + i);
  }
}

标签: 分页加载
分享给朋友:

相关文章

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…

分页实现vue

分页实现vue

分页实现方法(Vue) 使用Element UI分页组件 安装Element UI库后,可以直接使用其分页组件。在模板中添加el-pagination,通过v-model绑定当前页码,设置page-s…

vue实现分页条数

vue实现分页条数

Vue 实现分页条数控制 在 Vue 中实现分页条数控制通常需要结合分页组件和后端 API 支持。以下是几种常见的实现方式: 使用 Element UI 的分页组件 Element UI 提供了强大…

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

vue怎样实现分页

vue怎样实现分页

Vue实现分页的方法 使用第三方库(如Element UI或Ant Design Vue) Element UI的Pagination组件可以直接集成到Vue项目中。安装Element UI后,在组件…

js实现图片加载

js实现图片加载

使用Image对象加载图片 通过JavaScript的Image对象可以动态加载图片,适用于需要预加载或动态插入图片的场景。 const img = new Image(); img.src =…