因为我们做的项目是:前端 + Ios/Android 合作完成的App,那么在做消息通知时,进入消息详情,再返回列表,需要实时刷新列表,
那么有的详情是跳转ios/Android的,并且返回头部是他们的。
(常用的返回方法:uni.navigateTo()、uni.redirectTo();如果这两种不管用时,可以用下面这种返回方法刷新列表
onLoad()
与onShow()
的区别:onLoad第一次进页面才加载,onshow只要进页面就加载
这时我遇到两个问题:
1.从详情,返回列表,没有刷新列表
解决方法:如果你的列表方法写到onLoad()
中那么是不会刷新列表的;而是需要放到onShow()
中;
页面跳转传参的话:onLoad先执行,onShow后执行,不用担心上页面跳转参数取不到,刷新列表放到onshow就行。
2.因为列表是tab切换
,直接在onShow
中调列表方法是会出现列表数据push重复的问题的,
解决方法:所以需要page赋值1,列表数组置空。
代码如下,包括问题1的问题
onLoad(options) {//监听页面加载,其参数为上个页面传递的数据,参数类型为Object(用于页面传参)
// let obj = JSON.parse(decodeURIComponent(options.obj)); //解析去掉/等符号,跳转接参
// console.log(obj,'接参对象')
// // this.token = obj.token,//请求头
// this.getAllMessage()
},
onShow(){//监听页面显示。页面每次出现在屏幕上都触发,包括从下级页面点返回露出当前页面
this.page = 1
this.messageList = []//数组 返回后再次进入页面,或者多次下拉刷新,page返回1页,数组置空,刷新列表实时显示红点
this.getAllMessage()
},
//页面滚动到底部的事件(不是scroll-view滚到底),常用于下拉下一页数据。
onReachBottom() {
if(this.collectionTypeFlag == 0){
if (this.loadingType == 1) {
uni.stopPullDownRefresh()
} else {
this.page = this.page + 1
this.getAllMessage()
}
}else if(this.collectionTypeFlag == 1){
if (this.loadingType == 1) {
uni.stopPullDownRefresh()
} else {
this.page = this.page + 1
this.getAllMessage()
}
}
},
methods: {
//tab切换,下面这种方法,页数返回第一页,用onReachBottom方法(不推荐),tab切换时,tab下页面返回切换前页数
collectionTypeActive(index) {
this.collectionTypeFlag = index
if(index==0){
this.page = 1
this.messageList = []//数组
this.loadingType = 0
this.getAllMessage()
}else if(index==1){
this.page = 1
this.messageList = []//数组
this.loadingType = 0
this.getAllMessage()
}
},
}