From 3394a6d7c9f5fb07c6497977deb54e57890ce926 Mon Sep 17 00:00:00 2001 From: smallwei <1634566606@qq.com> Date: Wed, 23 Mar 2022 11:32:49 +0800 Subject: [PATCH] feat:router cache --- ruoyi-ui/src/cache.js | 31 ++++++++++++++++++++++ ruoyi-ui/src/layout/components/AppMain.vue | 14 +++++----- ruoyi-ui/src/main.js | 2 +- 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 ruoyi-ui/src/cache.js diff --git a/ruoyi-ui/src/cache.js b/ruoyi-ui/src/cache.js new file mode 100644 index 000000000..a5f4bc941 --- /dev/null +++ b/ruoyi-ui/src/cache.js @@ -0,0 +1,31 @@ +import Vue from 'vue' +import store from './store' +Vue.mixin({ + beforeRouteLeave: function (to, from, next) { + let list = store.state.tagsView.visitedViews + if (this.$vnode) { + if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) { + if (this.$vnode.componentOptions) { + var cache = this.$vnode.parent.componentInstance.cache; + var keys = this.$vnode.parent.componentInstance.keys; + keys.forEach(key => { + let t = {} + let i = list.findIndex(tag => { + if (key.indexOf(tag.path) !== -1) { + t = tag; + return true; + } + return false; + }) + let meta = t.meta || {} + if (i == -1 || meta.noCache == true) { + keys.splice(i, 1); + delete cache[key]; + } + }) + } + } + } + next(); + } +}) \ No newline at end of file diff --git a/ruoyi-ui/src/layout/components/AppMain.vue b/ruoyi-ui/src/layout/components/AppMain.vue index 7cc667488..899a09333 100644 --- a/ruoyi-ui/src/layout/components/AppMain.vue +++ b/ruoyi-ui/src/layout/components/AppMain.vue @@ -1,7 +1,8 @@ <template> <section class="app-main"> - <transition name="fade-transform" mode="out-in"> - <keep-alive :include="cachedViews"> + <transition name="fade-transform" + mode="out-in"> + <keep-alive> <router-view :key="key" /> </keep-alive> </transition> @@ -12,10 +13,7 @@ export default { name: 'AppMain', computed: { - cachedViews() { - return this.$store.state.tagsView.cachedViews - }, - key() { + key () { return this.$route.path } } @@ -31,7 +29,7 @@ export default { overflow: hidden; } -.fixed-header+.app-main { +.fixed-header + .app-main { padding-top: 50px; } @@ -41,7 +39,7 @@ export default { min-height: calc(100vh - 84px); } - .fixed-header+.app-main { + .fixed-header + .app-main { padding-top: 84px; } } diff --git a/ruoyi-ui/src/main.js b/ruoyi-ui/src/main.js index ebd94b9d8..2036781b2 100644 --- a/ruoyi-ui/src/main.js +++ b/ruoyi-ui/src/main.js @@ -4,7 +4,7 @@ import Cookies from 'js-cookie' import Element from 'element-ui' import './assets/styles/element-variables.scss' - +import './cache.js' import '@/assets/styles/index.scss' // global css import '@/assets/styles/ruoyi.scss' // ruoyi css import App from './App'