在main.js监听路由
// 按需加载路由,监听路由变化逻辑处理 $route (to, from) { if (to.params.isActive) { this.$store.commit('d2admin/page/keepAlivePush', to.name) } else { this.$store.commit('d2admin/page/keepAliveRemove', to.name) } }
在app.vue设置keep-alive
<!-- 页面 --> <div class="d2-theme-container-main-body" flex-box="1"> <transition :name="transitionActive ? 'fade-transverse' : ''"> <keep-alive :include="keepAlive"> <router-view /> </keep-alive> </transition> </div>
注:组件的name必须和路由配置的name一致,否则会没有效果。