业务场景:
在a页面滑动列表,点击进入b页面,再返回a页面时,a页面保留滑动的位置和页面缓存。
解决思路:
使用路由的keepAlive方法,keepAlive为true就会保留页面缓存。(返回该页面不会执行created和mounted)。
- app.vue使用缓存机制。
- a页面跳转到b页面时,设置a页面的keepAlive为true、保存页面滚动高度。
- 路由守卫判断去的页面不是b页面就将keepAlive改为false,防止别的页面跳转到a页面还有缓存。
- 在activated恢复页面滚动高度。
解决方法:
1.app.vue。使用缓存机制。
<template>
<div id="app">
<keep-alive>
<router-view v-if="$route.meta.keepAlive"/>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive">
<!--因为用的是v-if 所以下面还要创建一个未缓存的路由视图出口-->
</router-view>
<