当前位置:网站首页>项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
2022-06-27 05:29:00 【大只兔】
最近项目遇到一个需求,要求实现从列表进入详情,详情页面点击后退,页面停留在进入详情时的列表也main,即实现后退不刷新的功能,但详情也可以改变列表数据,提交详情数据需要重新获取列表数据
首先这是一个移动端项目,使用的了两个tab来展示两个列表,一个是待评分,一个是已评分,待评分数据评分后就会变成已评分数据,所以就要实现详情页面返回,列表页面不变不刷新,提交评分数据,页面就要重新加载数据
上网搜了很多教程,大多数使用的是keep-alive来进行页面缓存,所以我也是采用的keep-alive的方法。
1、对需要缓存的页面添加meta参数
在路由的route文件夹下,这里我的routes路由配置是单独抽出来的一个文件,不抽出来的就是在router/index.js文件里面
const routes = [
{
path: '/XXX',
name: '组件名',
meta: {
keepAlive: true
},
component: 页面组件名称,
}
]
2、在router/index.js文件中添加
const router = new Router({
mode: 'history',
routes,
scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
}
if (from.meta.keepAlive) {
from.meta.savedPosition = document.body.scrollTop // 此处为记录的滚动条位置
}
from.meta.savedPosition = document.body.scrollTop
return {
x: 0, y: to.meta.savedPosition || 0 }
}
})
3、点击返回的方法,使用back
back会使页面不刷新
this.$router.back(-1)
4、在app.js中使用keep-alive
这里使用 :key=“$route.fullPath”, 会使组件强制不复用,因为如果没有这个属性,组件就会有一个复用性,而添加这个属性就是为了在页面提交修改数据时,能够对列表数据进行重新渲染
<div id="app">
<keep-alive >
<router-view v-if="$route.meta.keepAlive" :key="$route.fullPath"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
5、需要重新渲染数据时
在详情页面提交数据时,就需要重新获取数据
这里给列表页面组件路径添加一个随机query数据,因为前面的路由的 :key=“$route.fullPath” ,点击提交数据后,通过这个push返回就能够重新加载页面了,其他时候页面会保持一个缓存状态,不会刷新。
this.$router.push({
path: '/列表路径',
query: {
'randomID': 'id' + Math.random() },
})
边栏推荐
- Comprehensive application of OpenCV in contour detection and threshold processing
- leetcode-20. Valid parentheses -js version
- Netease cloud music params and encseckey parameter generation code
- How JQ gets the ID name of an element
- Codeforces Round #802 (Div. 2)
- Redis高可用集群(哨兵、集群)
- 【FPGA】UART串口_V1.1
- neo4j图数据库基本概念
- leetcode299周赛记录
- Microservice system design -- distributed cache service design
猜你喜欢

es6 0622三

Codeforces Round #802 (Div. 2)

neo4j图数据库基本概念

【FPGA】 基于FPGA分频,倍频设计实现

How JQ gets the reciprocal elements

流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)

Redis high availability cluster (sentry, cluster)

Implementation of easyexcel's function of merging cells with the same content and dynamic title

STM32 reads IO high and low level status

竣达技术丨多品牌精密空调集中监控方案
随机推荐
STM32 reads IO high and low level status
快速排序(非遞歸)和歸並排序
Tri rapide (non récursif) et tri de fusion
LeetCode-515. 在每个树行中找最大值
Double position relay rxmd2-1mrk001984 dc220v
AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法
Microservice system design -- distributed transaction service design
Basic concepts of neo4j graph database
Microservice system design -- microservice invocation design
Two position relay hjws-9440
流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
020 basics of C language: C language forced type conversion and error handling
011 C language basics: C scope rules
How pychart installs packages
双位置继电器DLS-34A DC0.5A 220VDC
[FPGA] UART serial port_ V1.1
leetcode-20. Valid parentheses -js version
ES6 0622 III
双位置继电器HJWS-9440
Netease cloud music params and encseckey parameter generation code