栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合SpringSecurity实现权限控制(九):快速实现页面缓存

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合SpringSecurity实现权限控制(九):快速实现页面缓存

系列文章目录
《SpringBoot整合SpringSecurity实现权限控制(一):实现原理》
《SpringBoot整合SpringSecurity实现权限控制(二):权限数据基本模型设计》
《SpringBoot整合SpringSecurity实现权限控制(三):前端动态装载路由与菜单》
《SpringBoot整合SpringSecurity实现权限控制(四):角色管理》
《SpringBoot整合SpringSecurity实现权限控制(五):用户管理》
《SpringBoot整合SpringSecurity实现权限控制(六):菜单管理》
《SpringBoot整合SpringSecurity实现权限控制(七):权限分配》
《SpringBoot整合SpringSecurity实现权限控制(八):实现多标签页》


本文目录
  • 一、问题描述
  • 二、页面缓存的实现方法
    • 2.1 keep-alive组件
    • 2.1 配合router-view使用
  • 三、页面缓存的具体实现
  • 四、实现效果
  • 五、源码

一、问题描述
  • 在上篇文章中,我们实现了多标签页,这些页面以标签的形式集成在同一个窗口显示,要想切换到某个页面或是关闭某个页面,用户只需要操作相应的标签即可,非常方便快捷。
  • 但实际使用过程中,却发现以下情况:我们打开角色管理查询出角色信息,又打开了用户管理,之后再次切换回角色管理,发现这个页面被重新加载刷新了。
二、页面缓存的实现方法 2.1 keep-alive组件
  • 我们不希望组件被重新渲染影响用户体验;或者处于性能考虑,避免多次重复渲染降低性能。而是希望组件可以缓存下来,维持当前的状态,这时候就可以用到keep-alive组件。

keep-alive 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。keep-alive 是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出现在组件的父组件链中。

  • 用法:

  

  • 在vue 2.1.0后的版本中新增includeexclude prop 允许组件有条件地缓存。二者都可以用逗号分隔字符串、正则表达式或一个数组来表示:
    – include - 字符串或正则表达,只有匹配的组件会被缓存
    – exclude - 字符串或正则表达式,任何匹配的组件都不会被缓存

  

2.1 配合router-view使用

router-view 组件是一个 functional 组件,渲染路径匹配到的视图组件。router-view渲染的组件还可以内嵌自己的 router-view,根据嵌套路径,渲染嵌套组件。

  • 用法:

  
    
  

三、页面缓存的具体实现
  1. 创建一个存储用户打开的Tab页面的状态管理器,用来存放用户打开的且需要缓存的多个页面
// src/store/modules/tabbar.js
const state = {
  cachedViews: []
}

const mutations = {
  // 添加缓存页面
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.includes(view.name)) return
    if (!view.meta.noCache) {
      state.cachedViews.push(view.name)
    }
  },
  // 删除缓存页面
  DEL_CACHED_VIEW: (state, view) => {
    for (const i of state.cachedViews) {
      if (i === view.name) {
        const index = state.cachedViews.indexOf(i)
        state.cachedViews.splice(index, 1)
        break
      }
    }
  },
  // 删除全部缓存页面
  DEL_ALL_CACHED_VIEWS: state => {
    state.cachedViews = []
  }

}

const actions = {

  addCachedView({ commit }, view) {
    commit('ADD_CACHED_VIEW', view)
  },

  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_CACHED_VIEW', view)
      resolve([...state.cachedViews])
    })
  },

  delAllCachedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_CACHED_VIEWS')
      resolve([...state.cachedViews])
    })
  }

}

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

  1. Tabbar组件中监控路由变化情况,将路由信息对应的页面与tabs panel列表进行绑定时,我们把该页面加入Tab状态管理器
 watch: {
    $route: {
      handler(to, form = null) {
        // 当路由改变时,检测该路由是否已经在打开的页面之中,如果不在,就添加进去
        if (to.meta) {
          this.pageCurrent = to.path
          var index = this.pageList.findIndex(value => {
            return value.name === to.path
          })
          if (index < 0) {
            this.pageList.push({ name: to.path, label: to.meta.title, icon: to.meta.icon })
          }
          // 将需要缓存的页面放入状态管理器
          const { name, meta } = this.$route
          if (name && meta !== undefined && meta.noCache !== undefined && !meta.noCache) {
            this.$store.dispatch('tabbar/addCachedView', this.$route)
          }
        }
      },
      immediate: true
    }
  }
  1. Tabbar组件中用户移除tab页时,我们把该页面从Tab状态管理器中移除
removeTab(targetName) {
      if (targetName === '/dashboard') return
      const tabs = this.pageList
      let activeName = this.pageCurrent
      if (activeName === targetName) {
        tabs.forEach((tab, index) => {
          if (tab.name === targetName) {
            const nextTab = tabs[index + 1] || tabs[index - 1]
            if (nextTab) {
              activeName = nextTab.name
            }
          }
        })
      }
      this.pageCurrent = activeName

      this.pageList = tabs.filter(tab => tab.name !== targetName)
      // 将需要缓存页面从状态管理器中移除
      this.$store.dispatch('tabbar/delCachedView', this.$route)
      this.$router.push({ path: activeName })
    }
  1. 修改主界面组件,实现有条件地对状态管理器中的页面进行缓存








四、实现效果

五、源码
  • 前端
    https://gitee.com/zhuhuix/startup-frontend
    https://github.com/zhuhuix/startup-frontend
  • 后端
    https://gitee.com/zhuhuix/startup-backend
    https://github.com/zhuhuix/startup-backend
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号