Vue 详情页返回列表页,不刷新页面,并且保留列表页原来滚动条所在位置

前言:

最近公司有个移动端H5的项目,是前后端分离的,所以前端就Vue + Vux来做,项目是用vue-cli3生成的,其中就有这样的一个需求,在新闻列表页中向下滑动后,随别打开一个新闻,进入该新闻的详情页面,当看完后,从详情页返回到新闻列表页。这时,列表页重新请求接口数据,重新渲染DOM,而且滚动条也回到了最顶上的第1个新闻了。这样不仅性能不好,还会影响用户的体验效果。

 

问题分析:

1、从新闻详情页面返回新闻列表页后,新闻列表页重新渲染了DOM。

2、如果在新闻列表页向下滑动后,才进入新闻详情页面时,返回新闻列表页后滚动条回到最顶上了。

 

实现思路:

1、通过<keep-alive>容器组件,将页面缓存起来。

2、当路由跳转到其他页面时: beforeRouteLeave()方法,和 beforeRouteEnter()方法, 来记录 和 恢复滚动条的位置。

 

解决方法:

1、在router.js文件中,给News新闻列表页路由中添加 meta: {   keepAlive: true    //开启缓存  },代码如下:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/Main',
      name: 'Main',
      component: () => import('../views/Main.vue')
    },
    {
      path: '/',
      name: 'News',
      component: () => import('../views/News.vue'),
      meta: {
        keepAlive: true    //开启缓存
      }
    },
    {
      path: '/Info',
      name: 'Info',
      component: () => import('../views/Info.vue')
    }
  ]
})

 

2、在App.vue文件中,在<template>中的<router-view />用<keep-alive>包裹起来,代码如下:

<template>
  <div id="app">
    <keep-alive>
          <!-- 如果当前打开页面的路由中 keepAlive: true (开启了缓存时) -->
          <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
        
    <!-- 如果当前打开页面的路由中 没有 或者为 keepAlive: false (关闭缓存时[默认就是false]) -->
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

通过以上的两步设置以后,从新闻详情页面再返回新闻列表页时,新闻列表页就不会再重新渲染DOM了。

 

3、在News.vue页面中,添加以下两个监听方法,用于记录离开新闻列表页面时滚动条所在的位置,当返回新闻列表页面时设置滚动条的位置为之前所记录的值。


// 离开路由之前执行的函数

  beforeRouteLeave(to, from, next) {

    // 如果在window中出现的滚动条
    // this.scroll = window.scrollTop;

    //  如果在某个指的元素中出现的滚动条 就在该素中添加ref属性,如:ref="listBox"
    this.scroll = this.$refs.listBox.scrollTop;
    
    next()
  },
  



  // 进入路由之前执行的函数

  beforeRouteEnter(to, from, next) {
    next(vm => {

      // 如果在window中出现的滚动条
      // window.scrollTop = vm.scroll;

      // 如果在某个指的元素中出现的滚动条 就在该素中添加ref属性,如:ref="listBox"
      vm.$refs.listBox.scrollTop = vm.scroll
    })
  }

注:我的滑动区域是在指定的div元素中出现的滚动条

通过以上的三步设置以后,从新闻详情页面再返回新闻列表页时,新闻列表页就不会再重新渲染DOM,返回列表页后,滚动条也回到了原来的位置。

 

扩展:

如何在vue中实时查看滚动条,滚动的位置的值。

 mounted() {
    // 监听window中滚动条的位置
    window.addEventListener('scroll', function(){
     let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
      console.log(scrollTop);
    });

    // 监听指定某个元表滚动条的位置
    document.querySelector('#listBox').addEventListener('scroll', function(){
      let scrollTop = this.pageYOffset || this.scrollTop;
      console.log(scrollTop);
}

 

  • 4
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Vue3 移动端的列表页进入详情页后,为了让用户体验更加流畅,通常需要在用户返回列表页时保持原来滚动位置。下面是一种实现方式: 1. 在列表页中记录当前滚动位置 我们可以在列表页的 mounted 钩子函数中记录当前滚动位置,然后在用户返回列表页时,将页面滚动到该位置。 ```js <template> <div ref="listContainer"> <!-- 列表内容 --> </div> </template> <script> export default { mounted() { // 记录当前滚动位置 this.scrollPosition = 0; this.$refs.listContainer.addEventListener('scroll', this.onScroll); }, beforeUnmount() { // 移除滚动事件监听器 this.$refs.listContainer.removeEventListener('scroll', this.onScroll); }, methods: { onScroll() { // 更新滚动位置 this.scrollPosition = this.$refs.listContainer.scrollTop; }, }, }; </script> ``` 2. 在详情页中记录返回时的滚动位置 当用户进入详情页时,我们需要记录用户返回列表页时的滚动位置。我们可以使用路由的 meta 属性来记录该位置,然后在用户返回列表页时,从 meta 中获取该位置。 ```js const router = createRouter({ routes: [ { path: '/list', component: List, }, { path: '/detail/:id', component: Detail, meta: { scrollPosition: 0, }, }, ], }); <script> export default { mounted() { // 将返回时的滚动位置保存到路由的 meta 中 this.$route.meta.scrollPosition = this.$refs.detailContainer.scrollTop; }, }; </script> ``` 3. 在列表页中恢复滚动位置 当用户从详情返回列表页时,我们需要从路由的 meta 中获取返回时的滚动位置,然后将页面滚动到该位置。 ```js <template> <div ref="listContainer"> <!-- 列表内容 --> </div> </template> <script> export default { mounted() { // 从路由的 meta 中获取返回时的滚动位置 this.scrollPosition = this.$route.meta.scrollPosition || 0; this.$refs.listContainer.addEventListener('scroll', this.onScroll); }, beforeUnmount() { // 保存滚动位置到路由的 meta 中 this.$route.meta.scrollPosition = this.$refs.listContainer.scrollTop; // 移除滚动事件监听器 this.$refs.listContainer.removeEventListener('scroll', this.onScroll); }, methods: { onScroll() { // 更新滚动位置 this.scrollPosition = this.$refs.listContainer.scrollTop; }, }, activated() { // 页面重新激活时,将页面滚动到之前的位置 this.$refs.listContainer.scrollTop = this.scrollPosition; }, }; </script> ``` 通过上述步骤,我们就可以在 Vue3 移动端的列表页进入详情页后,保持原来滚动位置。需要注意的是,如果列表页中有异步加载的数据或者图片,可能会影响滚动位置的计算,需要特别注意。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值