zoukankan      html  css  js  c++  java
  • vue-router中scrollBehavior的巧妙用法

    原文链接https://blog.csdn.net/q3254421/article/details/84777614

    问题:使用keep-alive标签后部分安卓机返回缓存页位置不精确问题

    解决方案:

    <div id="app">
      <keep-alive>
       <router-view v-if="$route.meta.keepAlive"></router-view>
      </keep-alive>
      <router-view v-if="!$route.meta.keepAlive"></router-view>
    </div>
    const router = new Router({
     scrollBehavior(to, from, savedPosition) {
      if (savedPosition && to.meta.keepAlive) {
       return savedPosition;
      }
      return { x: 0, y:0 };
     },
    });
    1. 页面返回出现空白屏问题

    问题

    【前提】:iOS设备
    【步骤】: 页面A是个列表很长–>滑到页脚的时候点击跳转之后到页面B—>再返回A页面
    —>屏幕会出现空白遮罩层—>手指轻触屏幕滑动—>遮罩层消失

    解决方案一

    在接口请求成功后的回调操作完成后进行该操作,例如

    // fetchCourseList是一个封装好的Promise请求
    fetchCourseList().then(({ data: courses }) => {
     this.courses = courses;
    }).then(() => {
      setTimeout(() => {
        window.scrollTo(0, 1);
        window.scrollTo(0, 0);
      });
    });

    该方案的弊端: 每个页面都需要做这样的处理,不推荐使用。

    解决方案二(推荐)

    使用scrollBehavior中的异步滚动操作

    const router = new Router({
     scrollBehavior(to, from, savedPosition) {
      // keep-alive 返回缓存页面后记录浏览位置
      if (savedPosition && to.meta.keepAlive) {
       return savedPosition;
      }
      // 异步滚动操作
      return new Promise((resolve) => {
       setTimeout(() => {
        resolve({ x: 0, y: 1 });
       }, 0);
      });
     },
    });
  • 相关阅读:
    利用 SASS 简化 `nth-child` 样式的生成
    `http-equiv` meta 标签
    Currying 及应用
    理解 Redux 的中间件
    git clone 仓库的部分代码
    JavaScript Map 和 Set
    C++ 变量判定的螺旋法则
    CSS transition 的默认值
    `MediaDevices.getUserMedia` `undefined` 的问题
    MediaDevices.getUserMedia` undefined 的问题
  • 原文地址:https://www.cnblogs.com/zyx-blog/p/15061722.html
Copyright © 2011-2022 走看看