首页 > 其他分享 >若依框架 解决(菜单之间跳转,清除缓存。跳转到子页面在回来,缓存还在问题)

若依框架 解决(菜单之间跳转,清除缓存。跳转到子页面在回来,缓存还在问题)

时间:2022-11-04 16:11:23浏览次数:70  
标签:到子 缓存 visitedViews state resolve 跳转 commit cachedViews view

页面中使用

 

 代码

  beforeRouteLeave(to, from, next) {
    console.log("to", to);
    if (to.path == "/conentReview/infoDetailPage/index") {
      // 缓存页面
      let view = { meta: { noCache: false }, name: "systemRetrieval" };
      this.$store.dispatch("tagsView/addCachedView", view).then(() => {
        next();
      });
    } else {
      // 清除缓存页面
      this.$store
        .dispatch("tagsView/delCachedView", {
          meta: { noCache: false },
          name: "systemRetrieval",
        })
        .then(() => {
          next();
        });
    }
  },

tagsView是若依项目自带的(如下)

const state = {
    visitedViews: [],
    cachedViews: ["ManTopic"]
}
// , "themMangeChase"
const mutations = {
    ADD_VISITED_VIEW: (state, view) => {
        if (state.visitedViews.some(v => v.path === view.path)) return
        state.visitedViews.push(
            Object.assign({}, view, {
                title: view.meta.title || 'no-name'
            })
        )
    },
    ADD_CACHED_VIEW: (state, view) => {
        if (state.cachedViews.includes(view.name)) return
        if (!view.meta.noCache) {
            state.cachedViews.push(view.name)
        }
    },

    DEL_VISITED_VIEW: (state, view) => {
        for (const [i, v] of state.visitedViews.entries()) {
            if (v.path === view.path) {
                state.visitedViews.splice(i, 1)
                break
            }
        }
    },
    DEL_CACHED_VIEW: (state, view) => {
        const index = state.cachedViews.indexOf(view.name)
        index > -1 && state.cachedViews.splice(index, 1)
    },

    DEL_OTHERS_VISITED_VIEWS: (state, view) => {
        state.visitedViews = state.visitedViews.filter(v => {
            return v.meta.affix || v.path === view.path
        })
    },
    DEL_OTHERS_CACHED_VIEWS: (state, view) => {
        const index = state.cachedViews.indexOf(view.name)
        if (index > -1) {
            state.cachedViews = state.cachedViews.slice(index, index + 1)
        } else {
            state.cachedViews = []
        }
    },

    DEL_ALL_VISITED_VIEWS: state => {
        // keep affix tags
        const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
        state.visitedViews = affixTags
    },
    DEL_ALL_CACHED_VIEWS: state => {
        state.cachedViews = []
    },

    UPDATE_VISITED_VIEW: (state, view) => {
        for (let v of state.visitedViews) {
            if (v.path === view.path) {
                v = Object.assign(v, view)
                break
            }
        }
    }
}

const actions = {
    addView ({ dispatch }, view) {
        dispatch('addVisitedView', view)
        dispatch('addCachedView', view)
    },
    addVisitedView ({ commit }, view) {
        commit('ADD_VISITED_VIEW', view)
    },
    addCachedView ({ commit }, view) {
        commit('ADD_CACHED_VIEW', view)
    },

    delView ({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delVisitedView', view)
            dispatch('delCachedView', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },
    delVisitedView ({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_VISITED_VIEW', view)
            resolve([...state.visitedViews])
        })
    },
    delCachedView ({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_CACHED_VIEW', view)
            resolve([...state.cachedViews])
        })
    },

    delOthersViews ({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delOthersVisitedViews', view)
            dispatch('delOthersCachedViews', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },
    delOthersVisitedViews ({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_VISITED_VIEWS', view)
            resolve([...state.visitedViews])
        })
    },
    delOthersCachedViews ({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_CACHED_VIEWS', view)
            resolve([...state.cachedViews])
        })
    },

    delAllViews ({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delAllVisitedViews', view)
            dispatch('delAllCachedViews', view)
            resolve({
                visitedViews: [...state.visitedViews],
                cachedViews: [...state.cachedViews]
            })
        })
    },
    delAllVisitedViews ({ commit, state }) {
        return new Promise(resolve => {
            commit('DEL_ALL_VISITED_VIEWS')
            resolve([...state.visitedViews])
        })
    },
    delAllCachedViews ({ commit, state }) {
        return new Promise(resolve => {
            commit('DEL_ALL_CACHED_VIEWS')
            resolve([...state.cachedViews])
        })
    },

    updateVisitedView ({ commit }, view) {
        commit('UPDATE_VISITED_VIEW', view)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

 

标签:到子,缓存,visitedViews,state,resolve,跳转,commit,cachedViews,view
From: https://www.cnblogs.com/guohanting/p/16858189.html

相关文章

  • Spring Boot缓存实战 Redis 设置有效时间和自动刷新缓存,时间支持在配置文件中配置
    问题描述SpringCache提供的@Cacheable注解不支持配置过期时间,还有缓存的自动刷新。我们可以通过配置CacheManneg来配置默认的过期时间和针对每个缓存容器(value)单独配置过......
  • Caffeine 缓存
    简介在本文中,我们来看看 ​​Caffeine​​ —一个高性能的Java缓存库。缓存和Map之间的一个根本区别在于缓存可以回收存储的item。回收策略为在指定时间删除哪些对......
  • Spring Boot缓存实战 Caffeine
    Caffeine和SpringBoot集成Caffeine是使用Java8对Guava缓存的重写版本,在SpringBoot2.0中将取代Guava。如果出现Caffeine,CaffeineCacheManager将会自动配置。使用spring.ca......
  • Spring Boot缓存实战 Redis 设置有效时间和自动刷新缓存-2
    问题上一篇​​SpringBootCache+redis设置有效时间和自动刷新缓存,时间支持在配置文件中配置​​,说了一种时间方式,直接扩展注解的Value值,如:@Override@Cacheable(value=......
  • Spring Boot缓存实战 Redis + Caffeine 实现多级缓存
    在前文我们介绍了如何使用Redis或者Caffeine来做缓存。​​SpringBoot缓存实战Redis设置有效时间和自动刷新缓存-2​​​​SpringBoot缓存实战Caffeine​​问题描述:通......
  • @Cacheable注解实现Redis缓存
    SpringBoot缓存之@Cacheable介绍简介介绍:Spring从3.1开始就引入了对Cache的支持。定义了org.springframework.cache.Cache和org.springframework.cache.Cach......
  • Redis 先操作数据库和先删除缓存, 一致性分析
    初始状态:数据库和缓存中v=10 第一种,先删除缓存在操作数据库:线程1准备更新数据库的值v=20,先删除缓存,此时线程2进来,缓存未命中,查询数据库v=10,写入缓存v=10,......
  • 场景跳转
    执行方法//引入空间usingUnityEngine.SceneManagement;publicclassScenesJump{//name:要跳转的场景名字publicstaticvoidJump(stringname){......
  • vue跳转页面常用的几种方法
    vue跳转页面有好几种不同方法,下面将通过实例代码给大家介绍,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下。1、router-link跳转1.不带参数<router-lin......
  • Android实现页面跳转
    Android实现页面跳转​​MainActivity​​​绑定​​activity_main.xml​​​,​​Main2Activity​​​绑定​​activity_main2.xml​​​,则可以实现从​​activity_main.xml......