简体   繁体   English

Axios 从 GET 请求响应中设置 URL 为 GET 请求

[英]Axios set URL for GET request from the GET request response

This question is very similar to This question这个问题与这个问题非常相似

I have set up a Vue page with Laravel and showing all posts with a help of a GET request.我已经使用 Laravel 设置了一个 Vue 页面,并在 GET 请求的帮助下显示所有帖子。 I am also listening to a Laravel ECHO event and unshifting the value to the all posts array making it appear on top.我还在收听unshifting ECHO 事件并将值取消移动到所有帖子数组,使其显示在顶部。

I have set up the infinite scroll and paginating 5 results per page using this package .我已经使用此 package设置了无限滚动并为每页分页 5 个结果。 Results appear on the page and pushing to the array from the listener also works.结果显示在页面上,并且从侦听器推送到数组也可以。 However, when infinite scroll loads the 2nd results page, the 6th result is duplicated.但是,当无限滚动加载第 2 个结果页面时,会重复第 6 个结果。

The aforementioned package accepts next_cursor an offset value as the parameter instead of page=2 so it exactly loads the value without any duplications.前面提到的 package 接受next_cursor一个偏移值作为参数,而不是page=2 ,因此它完全加载该值而没有任何重复。

Controller.php Controller.php

    public function pusherGet(Request $request) {
        $jobs = Job::orderBy('id','desc')->cursorPaginate();        
        return response()->json($jobs);
    }

Vue file Vue文件

    <template>
        <div>
            <h3 class="text-center">All Jobs</h3><br/>
            <div class="container">
                <div class="card" v-for="(job,index) in jobs" :key="index">
                    <div class="card-body">
                        <h4 class="card-title">{{ job.id }}</h4>
                        <p class="card-text">{{ job.request_type}}</p>
                    </div>
                </div>    
            </div>
            <infinite-loading  @infinite="getJob"></infinite-loading>
        </div>
    </template>
    <script>
        export default {
            data() {
                return {
                   page:1,
                   jobs: [],                
                }
            },       
            mounted() {           
                this.listenNewJobs();  
            },
            created() {
           
            },
            methods: {            
                listenNewJobs() {
                    Echo.channel('chat-room.1')
                        .listen('JobCreated', (e) => {
                            console.log(e);                        
                            this.jobs.unshift(e.job);                            
                        });
                    },
                    getJob($state) {
                        axios.get('getjobs', {                
                            params: {
                                page: this.page,                        
                            },
                       }).then(({data})=> {
                           console.log(data) 
                           if(data.data.length) {
                               this.page += 1;
                               this.jobs.push(...data.data)
                               $state.loaded();
                            } else {
                            $state.complete();
                        }
                    });                     
                }
            }        
        }
    </script>

Results Json结果 Json

    {
        data: Array(5), path: "getjobs?page=1", previous_cursor: "100", next_cursor: "96", per_page: 5, …}
        data: (5) [{…}, {…}, {…}, {…}, {…}]
        next_cursor: "96" // this is the parameter which i should attach to the GET request to paginate correct results
        next_page_url: "getjobs?page=1&next_cursor=96"
path: "getjobs?page=1"
        per_page: 5
        prev_page_url: "getjobs?page=1&previous_cursor=100"
previous_cursor: "100"
        __proto__: Object

Any help would be greatly appreciated.任何帮助将不胜感激。

Edit: How to Set the URL for the GET request to paginate the results from the GET request response for paginated results to avoid 2nd page result duplication?编辑:如何为 GET 请求设置 URL 以对分页结果的 GET 请求响应的结果进行分页以避免第二页结果重复?

Try the following:尝试以下操作:

<script>
  export default {
    data() {
      return {
        jobs: [],
        isInitialLoaded: false,
        currentPage: 1,
        lastPage: 0,
      }
    },
    mounted() {
      this.listenNewJobs();
    },
    created() {
      //
    },
    methods: {
      listenNewJobs() {
        Echo.channel('chat-room.1')
          .listen('JobCreated', (e) => {
              console.log(e);
              this.jobs.unshift(e.job);
        });
      },
      async getJob($state){
        await this.fetchData().then((response) => {
          this.lastPage = response.data.last_page;
          if (response.data.data.length > 0) {
            response.data.data.forEach((item) => {
              const exists = this.jobs.find((job) => job.id == item.id);
              if (!exists) {
                // this.jobs.unshift(item); // Add to front of array
                this.jobs.push(item);
              }
            });
            if (this.currentPage - 1 === this.lastPage) {
              this.currentPage = 2;
              $state.complete();
            } else {
              this.currentPage += 1;
            }
            $state.loaded();
          } else {
            this.currentPage = 2;
            $state.complete();
          }
        });
        this.isInitialLoaded = true;
      },
      fetchData() {
        const url = this.isInitialLoaded ? `/getjobs?page=${this.currentPage}` : `/getjobs`;
        return axios.get(url);
      },
    }
  }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM