为什么数字分页在Vue中不起作用?

yqkkidmi  于 2023-08-07  发布在  Vue.js
关注(0)|答案(1)|浏览(194)

我的Vue应用程序在Vuex上遇到了一个问题,所以我正在进行分页,问题是它不显示我从商店中获得的事件,也不显示分页中的数字。问题还在于,在没有事件的第0页上检测到分页,而在应该已经显示事件的第一页上没有检测到分页。有没有人好心告诉我哪里做错了?如果能帮助解决这个问题,我将不胜感激。

index.js

state: {
  limit: 12,
  page: 0,
  events: [],
},

mutations: {
  limit(state, limit) {
    state.limit = limit
  },

  page(state, page) {
   state.page = page
  },

  allEvents(state, allEvents) {
   state.events = allEvents;
  },
},

actions: {
  async fetchEvents({ commit, state }) {
      try {
        const response = await provider().clients.graphqlClientPortal.query({
          query: gql`
            query LiveEvents($limit: Int!, $page: Int!){
              liveEvents(pagination:{limit:$limit, page:$page}, order:{startsAt:{direction: DESC}}){
                total,
                data{
                  id,
                  name,
                  stub,
                  description,
                  mainImage{
                    id,
                    uri
                  },
                  location,
                  type,
                  layout
                  chat,
                  liveStream{
                    endsAt,
                    startsAt
                  }
                }
              }
            }
          `,
          variables: {
            limit: state.limit,
            page: state.page
          },
        });
        console.log(response.data)
        commit('allEvents', response.data.liveEvents.data);
        commit("limit", response.data.liveEvents.data.limit),
        commit("page", response.data.liveEvents.page)
        //commit("total", response.data.liveEvents.total)
      } catch (error) {
        console.log('error-message', error.graphQLErrors)
      }
    },
}

字符串

List.vue

<template>
// displaying events
   <div v-for="(event, index) in displayedPosts" :key="index">
     <h2>{{ event.name }}</h2>
     <p>{{ event.location }}</p>
     ...
   </div>

// pagination with buttons and numbers
<div>
   <ul>
     <li>
       <button type="button" v-if="page != 1" @click="page--"> Previous </button>
     </li>
     <li>
       <button type="button" v-for="pageNumber in pages.slice(page - 1, page + 5)" :key="pageNumber"
         @click="page = pageNumber"> {{ pageNumber }} </button>
     </li>
     <li>
       <button type="button" @click="page++" v-if="page < pages.length"> Next </button>
     </li>
   </ul>
 </div>
</template>

<script>
export default {
  name: "List",

  data() {
    return {
      posts: [],
      page: this.$store.state.page,
      perPage: this.$store.state.limit,
      pages: [],
    }
  },

  mounted() {
    this.$store.dispatch("fetchEvents")
  },

  methods: {
    getPosts() {
      let events = this.$store.state.events;
      for (let i = 0; i < events.length; i++) {
        this.posts.push(events[i])
      }
    },

    setPages() {
      let numberOfPages = Math.ceil(this.posts.length / this.perPage);
      for (let index = 1; index <= numberOfPages; index++) {
        this.pages.push(index);
      }
    },

    paginate(posts) {
      let page = this.page;
      let perPage = this.perPage;
      let from = (page * perPage) - perPage;
      let to = (page * perPage);
      return posts.slice(from, to);
    }
  },

  computed: {
    displayedPosts() {
      return this.paginate(this.posts);
    }
  },

  watch: {
    posts() {
      this.setPages();
    }
  },

  created() {
    this.getPosts();
  },

  filters: {
    trimWords(value) {
      return value.split(" ").splice(0, 20).join(" ") + '...';
    }
  }
}
</script>

w1e3prcc

w1e3prcc1#

1.您没有正确更新Vuex存储中的pagelimit。你试图用错误的数据提交变异。limitpage不是liveEvents.data对象的一部分,它们是您在查询中发送的变量的一部分。
1.您没有查看Vuex存储中events状态的更改。这意味着当events状态发生变化时,您的组件不会对这些变化做出React。
1.在data函数中初始化posts,然后在getPosts方法中将其推送到posts。这不是React性的,Vue将无法跟踪posts的更改。
以下是解决这些问题的方法:
1.更新fetchEvents操作以提交正确的limitpage

actions: {
  async fetchEvents({ commit, state }) {
    // ...
    commit('allEvents', response.data.liveEvents.data);
    commit("limit", state.limit),
    commit("page", state.page)
    // ...
  },
}

字符串
1.观察组件中的Vuex events状态,并在其更改时更新posts

watch: {
  '$store.state.events': {
    handler: function(events) {
      this.posts = events;
    },
    immediate: true,
  },
  posts() {
    this.setPages();
  }
},


1.删除getPosts方法和created生命周期钩子,因为不再需要它们。
1.在Vuex商店中将page初始化为1而不是0:

state: {
  limit: 12,
  page: 1,
  events: [],
},


1.更新paginate方法以处理page为0的情况:

paginate(posts) {
  let page = this.page;
  let perPage = this.perPage;
  let from = (page * perPage) - perPage;
  let to = (page * perPage);
  return page > 0 ? posts.slice(from, to) : [];
}


1.更新Previous按钮中的v-if条件,以检查page是否大于1:

<button type="button" v-if="page > 1" @click="page--"> Previous </button>


通过这些更改,您的分页应该可以正常工作。

相关问题