当前位置:首页 > VUE

vue实现商品分页

2026-01-19 17:02:26VUE

Vue 实现商品分页的方法

基础分页实现

安装必要的依赖(如axios用于请求数据)

npm install axios

创建分页组件模板

<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 }} 页 / 共 {{ totalPages }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

数据逻辑处理

设置初始数据状态

data() {
  return {
    items: [],
    currentPage: 1,
    itemsPerPage: 10,
    totalItems: 0
  }
}

计算总页数

computed: {
  totalPages() {
    return Math.ceil(this.totalItems / this.itemsPerPage)
  }
}

API请求方法

实现获取分页数据的方法

vue实现商品分页

methods: {
  async fetchItems() {
    try {
      const response = await axios.get('/api/products', {
        params: {
          page: this.currentPage,
          limit: this.itemsPerPage
        }
      })
      this.items = response.data.items
      this.totalItems = response.data.total
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  },
  nextPage() {
    if (this.currentPage < this.totalPages) {
      this.currentPage++
      this.fetchItems()
    }
  },
  prevPage() {
    if (this.currentPage > 1) {
      this.currentPage--
      this.fetchItems()
    }
  }
}

生命周期钩子

在组件挂载时加载初始数据

mounted() {
  this.fetchItems()
}

样式优化

添加基础分页样式

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

button {
  padding: 5px 10px;
  cursor: pointer;
}

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

高级分页功能

实现页码跳转功能

vue实现商品分页

<input 
  type="number" 
  v-model.number="inputPage" 
  min="1" 
  :max="totalPages"
  @keyup.enter="goToPage"
>
<button @click="goToPage">跳转</button>

添加对应方法

data() {
  return {
    inputPage: 1
  }
},
methods: {
  goToPage() {
    const page = Math.max(1, Math.min(this.inputPage, this.totalPages))
    if (page !== this.currentPage) {
      this.currentPage = page
      this.fetchItems()
    }
  }
}

性能优化

添加加载状态

data() {
  return {
    isLoading: false
  }
},
methods: {
  async fetchItems() {
    this.isLoading = true
    try {
      // ...原有请求代码
    } finally {
      this.isLoading = false
    }
  }
}

在模板中添加加载指示器

<div v-if="isLoading">加载中...</div>

服务器端分页

确保后端API支持分页参数

// 示例Node.js后端处理
router.get('/api/products', async (req, res) => {
  const page = parseInt(req.query.page) || 1
  const limit = parseInt(req.query.limit) || 10
  const skip = (page - 1) * limit

  const items = await Product.find().skip(skip).limit(limit)
  const total = await Product.countDocuments()

  res.json({ items, total })
})

标签: 分页商品
分享给朋友:

相关文章

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。需要先安装Element UI库。 <…

vue 分页的实现

vue 分页的实现

Vue 分页的实现方法 使用 Element UI 的分页组件 安装 Element UI: npm install element-ui 在 Vue 项目中引入 Element UI 的分页组件:…

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

分页用vue实现

分页用vue实现

分页用 Vue 实现 在 Vue 中实现分页功能通常需要结合数据管理和 UI 组件。以下是几种常见的方法: 使用 Vue 组件库的分页组件 许多 Vue UI 组件库(如 Element UI、V…

vue前端分页怎么实现

vue前端分页怎么实现

使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 <el-pagination>,可以快速实现分页功能。需要安装 Element UI 并注册组件。 &…