ITeye博客

解决vue使用keep-alive三级页面缓存问题

xiaotongeye · 2021-08-31 18:24 · vue · Web前端 · Markdown

**需求描述:**管理后台存在三级页面,列表页和详情页,列表页缓存,详情页不缓存

**实现:**使用keep-alive的include,两级页面都需要添加keep-alive,借鉴vue-element-admin tags-view组件的visitedViews状态对缓存页面进行管理

// home.vue
<keep-alive :include="cachedViews">
    <router-view :key="key"></router-view>
</keep-alive>

computed: {
      key() {
        return this.$route.fullPath
      },
      cachedViews() {
        return this.$store.state.tagsView.cachedViews
      },
},

 

// 二级页面
<keep-alive>
      <router-view></router-view>
</keep-alive>

 

// 全局钩子
beforeRouteEnter(to, from, next) {
    // 解决三级菜单缓存问题
    if (to.matched&&to.matched.length > 2) {
      for(let i = 1; i < to.matched.length; i ++) {
        const element = to.matched[i]
        if (pageArr.includes(element.components.default.name)) {
          to.matched.splice(i, 1)
        }
      }
    }
    next()
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。