在uni中,返回页面是不会触发onLoad
方法的;
如果我们只想在特定情况下返回上一页才需要刷新数据,那么用onShow
的话,那刷新就太频繁了;
这时候,可以用$emit
和$on
去解决。
比如说,从详情页(detail.vue) 回到 列表页(list.vue):
详情页(detail.vue):
methods:{
back() {
let a = 1
//携带参数
uni.$emit('refreshData',a);
uni.navigateBack(1)
}
}
列表页(list.vue):
onLoad() {
// 正常进入该页面的获取数据
this.getList();
// 从详情页返回该页面的获取数据
uni.$on('refreshData',res => {
console.log(res); //接收参数 a
this.getList();
})
},
methods:{
getList() {
// 请求数据接口
... ...
}
}