一开始没想出来纠结好久,后来突然开窍了,进入详情页就判断下,列表判断当前的位置 前面的就是上一个 后面的就是下一个,判断当前页的索引位置 是不是第一或者最后个【简单手写】
// 当前页
async newsContent() {
const all = this.xw;
const res = await news_content({ id: this.id });
const { title, id } = res.data.data;
this.title = res.data.data;
this.title1 = title;
let index = all.findIndex((value) => value.id == id); //索引
console.log(222, index);
const allLen = all.length - 1;
if (index == 0) {
// 没有上一页
this.title1 = "没有上一页";
// 下一篇
this.next1 = all[index + 1];
this.title2 = this.next1.title;
} else if (index == allLen) {
// 没有下一页
this.title2 = "没有下一页";
// 上一篇
this.prev1 = all[index - 1];
this.title1 = this.prev1.title;
} else {
// 上一篇
this.prev1 = all[index - 1];
this.title1 = this.prev1.title;
// 下一篇
this.next1 = all[index + 1];
this.title2 = this.next1.title;
}
},
// 新闻列表
async index() {
const data = await index({ page: this.page });
// console.log("index", data);
this.total = data.data.data;
this.first = data.data.data.first;
this.xw = data.data.data.list;
this.newsContent(); //当前页
},