keep-alive(某一个页面我从部分路径过来,需要缓存,但从另一部分路径过来,不能缓存页面,需要刷新)
- 这个问题我从网上找到一个比较好的方法解决:https://blog.youkuaiyun.com/GraffitiBoy/article/details/106812435
关键代码如下表:
父组件
//首先添加v-if,并且写个祖宗方法让其子页面调用v-if销毁创建的方法
<template>
<div id="app">
<keep-alive v-if="isRouterAlive">
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
<script>
export default {
name: "App",
provide() {
return {
reload: this.reload
};
},
data() {
return {
isRouterAlive: true
};
},
methods: {
reload() {
this.isRouterAlive = false;
this.$nextTick(() => (this.isRouterAlive = true));
}
}
};
</script>
子组件
//在新增页面上,调用此祖宗方法,销毁创建,就可以重新刷新新增页面数据
export default {
inject: ["reload"],
beforeRouteEnter(to, from, next) {
if (from.fullPath === "/SecurityCheck") {
next(vm => {
vm.reload();
});
}
},
}