You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Vue.js简易分页插件实现API数据分页布局时遇到问题求助

问题修正及实现代码

以下是修复配置错误、新增flex布局后的完整可运行代码,改动点如下:

  • 补全VuePaginate相关组件注册逻辑
  • 修正paginate配置项的字符串引号、数据绑定对应关系
  • 补全接口请求地址的引号,适配接口返回的标准数据结构
  • 统一paginate和paginate-links的绑定名称,paginate组件可通过per属性自定义每页展示数量,默认值为3
  • 新增flex布局样式,实现列表弹性排列效果
<template>
  <div id="app">
    <paginate name="articles" :list="articles" :per="9" class="paginate-list">
      <li v-for="item in paginated('articles')" :key="item.id" class="article-item">
        <!-- 可根据接口实际返回字段调整展示内容 -->
        {{ item.title }}
      </li>
    </paginate>
    <!-- 按需保留你需要的分页控件,不需要的可直接删除 -->
    <paginate-links for="articles" :show-step-links="true" class="paginate-bar"></paginate-links>
    <!-- <paginate-links for="articles" :limit="2" :show-step-links="true" class="paginate-bar"></paginate-links>
    <paginate-links for="articles" :simple="{ next: '下一页 »', prev: '« 上一页' }" class="paginate-bar"></paginate-links> -->
  </div>
</template>

<script>
import axios from 'axios';
import VuePaginate from 'vue-paginate'
// 注册分页相关组件
const { Paginate, PaginateLinks } = VuePaginate.components

export default {
  components: { Paginate, PaginateLinks },
  data() {
    return {
      articles: [],
      // 分页配置和paginate标签的name属性对应,注意字符串要加引号
      paginate: ['articles']
    }
  },
  created() {
    // 补全请求地址引号,赋值接口返回的列表数据
    axios.get('https://zbeta2.mykuwaitnet.net/backend/en/api/v2/media-center/press-release/?page_size=61&type=5')
      .then(response => {
        // 可根据接口实际返回结构调整字段取值
        this.articles = response.data.results || response.data
      })
  }
}
</script>

<style scoped>
.paginate-list {
  margin: 0;
  padding: 0;
  list-style: none;
  /* flex布局核心配置 */
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 24px;
}
.article-item {
  /* 按需求调整宽度,以下配置为一行展示3个卡片 */
  width: calc(33.33% - 11px);
  padding: 16px;
  border: 1px solid #eee;
  border-radius: 4px;
  box-sizing: border-box;
}
.paginate-bar {
  display: flex;
  justify-content: center;
  gap: 8px;
  list-style: none;
  padding: 0;
  margin: 0;
}
.paginate-bar a {
  padding: 4px 8px;
  border: 1px solid #eee;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  color: #333;
}
.paginate-bar li.active a {
  background: #409eff;
  color: #fff;
  border-color: #409eff;
}
</style>

内容的提问来源于stack exchange,提问作者Balazs Kelemen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 02:24:03