vue tab页面缓存处理

vue tab页面缓存处理

问题:使用vant 框架。底部导航切换,tab页面缓存

解决:
1.index 页面 xml 设置

<keep-alive>
 <router-view v-if="$route.meta.keepAlive" style="margin-bottom: 50px"/>
 </keep-alive>
<router-view v-if="!$route.meta.keepAlive" style=" margin-bottom: 50px"/>

注意: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.首页

<!--  -->
<template>
  <div>
    <div v-for="(item,index) in lists" :key="index">
      <div style="height: 50px;color: #449908;margin: 10px;background-color: #991908;text-align: center;line-height:50px" @click="toDetail(item)">
        {{ item }}
      </div>

    </div>
  </div>
</template>

<script>
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 < 50; i++) {
      this.lists.push('测试' + (i + 1))
    }
    console.log("Home===>created()")
  }

}
</script>
<style scoped>

</style>

b.详情页:

<!--  -->
<template>
  <div class=''>
    详情:{{ value }}
    滑动距离顶部位置:{{ scrollHeight }}
  </div>
</template>

<script>


export default {

  data() {

    return {
      value: '',
      scrollHeight: ''
    };
  },


  methods: {},

  created() {
    const item = this.$route.query.item;
    const position = this.$route.query.position;
    this.value = item
    this.scrollHeight = position
    console.log("列表页面传过来的值1:", item)
    console.log("列表页面传过来的值2:", position)
  },

  destroyed() {
    console.log("详情页面生命周期destroyed:")
    localStorage.setItem('position', this.scrollHeight)
  }


}
</script>
<style scoped>

</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值