1.uniapp 从列表进入详情页,再返回列表页时回到上次的浏览位置
javascript
<script>
export default {
data() {
return {
listData: [],
scrollTop: 0,//浏览位置
};
},
onShow() {
this.getShopData();
},
onPageScroll: function (e) {
// 当页面滚动时保存值
this.scrollTop = e.scrollTop;
},
onBackPress(options) {
//列表页点击页面左上角的返回按钮时从本地缓存中移除指定的key
if (options.from === 'backbutton') {
uni.removeStorage({
key: 'newsTop'
});
uni.navigateBack();
return true;
}
},
methods: {
getShopData() {
getShopList()
.then((res) => {
this.listData = res.data.list.data;
// 渲染完数据后将页面滚动到目标位置
uni.getStorage({
key: 'newsTop',
success: (res) => {
console.log(res);
if (res.data) {
uni.pageScrollTo({
scrollTop: res.data,
duration: 0,
});
}
},
});
})
.finally(() => {});
},
editShop(storeId = '') {
//跳转详情页时将最后的滚动距离存到缓存中
uni.setStorage({
key: 'newsTop',
data: this.scrollTop,
});
uni.navigateTo({
url: '/pages/edit/shop/shop?store_id=' + storeId,
});
},
},
};
</script>