export default {
data() {
return {
griddata:[], //每次加载出来的新数据
Data:[], //每次加载累加后的总数据
allLoaded: false, //若为真,则
bottomMethod
不会被再次触发
num:0, //num为0时表示刷新或第一次加载,每加载一次增长1,刷新时默认为0
readList:[], //红点标记
}
},
methods:{
goto_detail(payload){ //跳转到详情页
this.$router.push({path:'/detail'})
},
loadTop() { //下拉刷新
this.update(0,'top');
},
loadBottom() { //上拉加载
this.num+=1;
let offset = this.num*10 //offset为分页偏移量,这里是每次加载增长十条数据
this.update(offset,'bottom');
},
update(type){ //每次触发上拉加载或下拉刷新时触发的数据接口
let param = {
number:10, //每页多少条数据
}
news(param).then((ret) => {
if(ret.status_code==200){
if(offset==0){ //若是偏移量为0说明是下拉刷新因此Data为刚加载出来的数据
}else{ //偏移量大于0,说明为上拉加载,Data为前面锁加载出来数据的累加
this.griddata=ret.dataInfo
let len=this.griddata.length
for(let i=0;i<len;i++){
this.Data.push(this.griddata[i]) //将新数据push到Data中
}
}
let read = '' //read为接口获取的数据,为是否已读的标记,当为0时未读,为1时已读
this.readList=[] //新数组,放全部加载出数据的read值
for(let i=0;i<this.Data.length;i++){
this.readList.push(this.Data[i].read);
}
}
//经过传过来的type值不一样分辨上拉加载或下拉刷新
if(type=='top'){
this.$refs.loadmore.onTopLoaded();
}else if(type=='bottom'){
this.$refs.loadmore.onBottomLoaded();
}
})
}
},
updated(){ //只要数据变化就会判断一次数据是否已读过,判断标记的显隐
let oSpan=document.getElementById("ul-box").getElementsByTagName("span");
for(let i=0;i<this.readList.length;i++){
if(this.readList[i]==0){
oSpan[i].className='mark is-red'
}else{
oSpan[i].className='mark'
}
}
},
created(){ //打开页面首先自动获取一次数据
let param = {
offset: 0, //打开页面至关于初次加载
number:10, //每页多少条数据
}
news(param).then((ret) => {
if(ret.status_code==200){
let read = '' //read为接口获取的数据,为是否已读的标记,当为0时未读,为1时已读
this.readList=[] //新数组,放全部加载出数据的read值
for(let i=0;i<this.Data.length;i++){
this.readList.push(this.Data[i].read);
}
}
})
}
}
文章来源于 》》 竹生云野处 博客