vue中使用keep-alive 实现进入当前页不缓存,但返回到当前页缓存的功能
描述: A页面进入B页面,B页面进入C页面, C页面返回到B页面需要缓存到之前滑动的位置,但是A页面进入B页面的时候不需要缓存B页面之前滑动的位置
在app.vue中
<keep-alive :include="keepAlive">
<router-view />
</keep-alive>
computed: {
keepAlive () {
return this.$store.state.keepAlive
}
}
在store中
state: {
keepAlive: []
},
mutations: {
setKeepAlive: (state, keepAlive) => {
state.keepAlive = keepAlive
}
}
在页面中
beforeRouteLeave (to, from, next) {
if (to.path.indexOf('页面c') > -1) {
this.$store.commit('setKeepAlive', ['页面B的名字'])
} else {
this.$store.commit('setKeepAlive', [])
}
next()
}