vue tab页面缓存处理
问题:使用vant 框架。底部导航切换,tab页面缓存
解决: 1.index 页面 xml 设置
注意:style=" margin-bottom: 50px" 设置是为了底部数据不被导航按钮遮住,导航栏默认有高度
2.路由中设置 route.js
{
path: '/',
component: () => import('@/views/index'),
redirect: '/home',
meta: {
title: '首页',
keepAlive: true
},
children: [
{
path: '/home',
name: 'Home',
component: () => import('@/views/home/index'),
meta: {title: '首页', keepAlive: true}
},
{
path: '/mine',
name: 'Mine',
component: () => import('@/views/mine/index'),
meta: {title: '个人中心', keepAlive: true}
}
]
},
注意:keepAlive: true 代表页面需要缓存,切换页面只走一次 create()函数
3.其他问题 问题:如果首页是列表,切换tab 或者从列表进去详情,回到列表页 ,列表不会记录之前滑动的位置 解决:记录列表滑动的位置,离开页面后 保存到缓存,重新返回列表时,取出缓存的位置,设置列表指定到该位置。
核心代码如下:
a.首页
{{ item }}
export default {
name: 'Home',
data() {
return {
lists: [],
scrollValue:''
};
},
methods: {
toDetail(item){
this.scrollValue = window.scrollY
console.log("详情页")
this.$router.push({path: '/detail', query: {item: item,position: this.scrollValue}})
}
},
//离开当前路由
beforeRouteLeave(to, from, next) {
if (from.meta.keepAlive) {
console.log("首页滑动的位置" + window.scrollY)
this.scrollValue = window.scrollY
localStorage.setItem('position', this.scrollValue)
}
next();
},
activated() {
let position = localStorage.getItem('position')
this.$nextTick(() => { //必须使用nextTick(在下次 DOM 更新循环结束之后执行延迟回调)
if (position!==undefined){
window.scroll(0, position)
}
})
},
created() {
for (let i = 0; i
关注
打赏
热门博文
- kotlin使用retrofit请求报错Parameter type must not include a type variable or wildcard
- android 中Handle弱引用使用
- 颜色透明参数
- flutter 手机端抓包设置
- vite 创建项目
- 鸿蒙 加载本地rowfile文件中的 json 数据
- 鸿蒙 加载长图(大图自动适用屏幕的宽度)
- Android 与Vue 页面交互
- 鸿蒙 保存图片到相册
- 鸿蒙 上传图片路径转换dataability:///media/external/images/media/30为/storage/emulated/0/DCIM/YIIoT/5769014/202