Vue.js 使用Nuxt实现Previos和下一页

Vue.js 使用Nuxt实现Previos和下一页,vue.js,vuejs2,nuxt.js,Vue.js,Vuejs2,Nuxt.js,我在nuxt中执行上一个和下一个时遇到问题 在我的模板中,我有这个 <template> <div class="mt-8"> <div class="flex justify-between"> <h4 class="title text-orange-500 font-semibold"> <span>POPULAR MOVIES&l

我在nuxt中执行上一个和下一个时遇到问题

在我的模板中,我有这个

<template>
  <div class="mt-8">
    <div class="flex justify-between">
      <h4 class="title text-orange-500 font-semibold">
        <span>POPULAR MOVIES</span>
      </h4>
      <h4 class="title text-orange-500">
        <span>Page: {{ currentPage }} of {{ totalPages }}</span>
      </h4>
    </div>
    <div class="popular grid sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-8">
      <Moviecards
        v-for="(movie, index) in popularmovies"
        :key="index"
        :movie="movie"
        :data-index="index"
      />
    </div>
    <div class="flex space-x-4 justify-center mt-8">
      <span v-if="currentPage === null">
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-l ml-8 cursor-not-allowed">
          Prev
        </button>
      </span>
      <span v-else>
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-l ml-8" @click="currentPage--">
          Prev
        </button>
      </span>
      <span v-if="currentPage === totalPages">
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-r mr-8 cursor-not-allowed">
          Next
        </button>
      </span>
      <span v-else>
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-r mr-8" @click="next">
          Next
        </button>
      </span>
    </div>
  </div>
</template>
<script>
import Moviecards from '@/components/Moviecards.vue'

export default {
  components: {
    Moviecards
  },
  data () {
    return {
      prevpage: null,
      nextpage: null,
      currentPage: 1,
      pageNumbers: [],
      totalPages: 0,
      popularmovies: []
    }
  },
  methods: {
    next () {
      this.currentPage += 1
    },
    previous () {
      this.currentPage -= 1
    }
  },
  // eslint-disable-next-line vue/order-in-components
  async fetch () {
    const popularmovies = await fetch(
      `https://api.themoviedb.org/3/movie/popular?api_key=${process.env.API_SECRET}&page=${this.currentPage}`
    ).then(res => res.json())
    this.totalPages = popularmovies.total_pages
    this.popularmovies = popularmovies.results
  }

流行电影
页码:{{currentPage}}共{{totalPages}}
上
上
下一个
下一个
在我的剧本里我有

<template>
  <div class="mt-8">
    <div class="flex justify-between">
      <h4 class="title text-orange-500 font-semibold">
        <span>POPULAR MOVIES</span>
      </h4>
      <h4 class="title text-orange-500">
        <span>Page: {{ currentPage }} of {{ totalPages }}</span>
      </h4>
    </div>
    <div class="popular grid sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-8">
      <Moviecards
        v-for="(movie, index) in popularmovies"
        :key="index"
        :movie="movie"
        :data-index="index"
      />
    </div>
    <div class="flex space-x-4 justify-center mt-8">
      <span v-if="currentPage === null">
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-l ml-8 cursor-not-allowed">
          Prev
        </button>
      </span>
      <span v-else>
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-l ml-8" @click="currentPage--">
          Prev
        </button>
      </span>
      <span v-if="currentPage === totalPages">
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-r mr-8 cursor-not-allowed">
          Next
        </button>
      </span>
      <span v-else>
        <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded-r mr-8" @click="next">
          Next
        </button>
      </span>
    </div>
  </div>
</template>
<script>
import Moviecards from '@/components/Moviecards.vue'

export default {
  components: {
    Moviecards
  },
  data () {
    return {
      prevpage: null,
      nextpage: null,
      currentPage: 1,
      pageNumbers: [],
      totalPages: 0,
      popularmovies: []
    }
  },
  methods: {
    next () {
      this.currentPage += 1
    },
    previous () {
      this.currentPage -= 1
    }
  },
  // eslint-disable-next-line vue/order-in-components
  async fetch () {
    const popularmovies = await fetch(
      `https://api.themoviedb.org/3/movie/popular?api_key=${process.env.API_SECRET}&page=${this.currentPage}`
    ).then(res => res.json())
    this.totalPages = popularmovies.total_pages
    this.popularmovies = popularmovies.results
  }

从“@/components/Moviecards.vue”导入电影卡
导出默认值{
组成部分:{
电影片
},
数据(){
返回{
前页:空,
下一页:空,
当前页面:1,
页码:[],
总页数:0,
大众生活:[]
}
},
方法:{
下一个(){
此.currentPage+=1
},
前(){
此.currentPage-=1
}
},
//eslint禁用下一行vue/订购组件
异步获取(){
const popularmovies=等待获取(
`https://api.themoviedb.org/3/movie/popular?api_key=${process.env.API_SECRET}&page=${this.currentPage}`
).then(res=>res.json())
this.totalPages=popularmovies.total_页面
this.popularmovies=popularmovies.results
}

下一个方法有效,当前页面增加1,前一个减少1。但是我无法更新API调用URL,我假设它是在请求页面时加载的。我需要帮助找到解决方法。谢谢。

您可以使用以下代码来解决此问题

let pages=getCurrentPages();
让beforePages=pages[pages.length-2];//beforePages对象

console.log(beforePages);
也许您可以尝试以下代码,这些代码可能适用于您的问题


  • 主要
  • 以前的
  • {{index}}
  • 下一个
  • 最后一页
导出默认值{ 名称:“AppPager”, 道具:{ url:{//分页链接 必填项:false, 类型:[字符串], 默认值:null }, 总数:{ 必填项:false, 类型:[编号], 默认值:0 }, 第页:{ 必填项:false, 类型:[数字,字符串], 默认值:0 } }, 数据(){ 返回{ 当前:1, 展览项目:5,, 页面大小:8 }; }, 计算:{ 第()页{ 设pag=[]; //起始号码 设左=1; //结束号码 const totalPage=this.totalPage; 设right=totalPage; 让middle=Math.ceil(this.showItem/2); 如果(totalPage>=此.showItem){ 在中间,我们把它加到两边(中间-1)。 如果(this.current>middle&&this.current我不明白这个代码,你能添加更多细节吗?请查看我的最新评论