Vue-Router前端页面缓存

696 阅读1分钟

情景: 页面不需要实时更新,进入页面把页面载入内存,减少不必要的请求,减轻后台压力

  1. 添加TagesView组件(官网复制)

export { default as TagsView } from './TagsView/index.vue'

import { TagsView } from './components'

  1. 添加TagesView的全局变量vuex管理

import tagsView from './modules/tagsView'

  1. 需要缓存的组件放在keep-alive中 AppMain.vue
<keep-alive :include="cachedViews">
  <router-view :key="key" />
</keep-alive>

computed: {
  cachedViews() {
    return this.$store.state.tagsView.cachedViews
  }
}
  1. 路由默认缓存,不缓存需添加noCache: true