zoukankan      html  css  js  c++  java
  • VUE 动态菜单管理

    业务场景

    不同的用户登录,看到的菜单会不一样,因此需要根据不同人登录的身份去后端获取菜单。

    实现思路

    1.构建路由

    2.从后端构建菜单

    3.前端获取菜单

    4.前端渲染菜单

    1.构建路由。

    export const constantRoutes = [
      {
        path: '/login',
        component: () => import('@/views/login/index'),
        hidden: true
      },
    
      {
        path: '/404',
        component: () => import('@/views/404'),
        hidden: true
      },
    
      {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [{
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/dashboard/index'),
          meta: { title: 'Dashboard', icon: 'dashboard' }
        }]
      },

    构建路由

    const createRouter = () => new Router({
      // mode: 'history', // require service support
      scrollBehavior: () => ({ y: 0 }),
      routes: constantRoutes
    })
    
    const router = createRouter()
    
    // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
    export function resetRouter() {
      const newRouter = createRouter()
      router.matcher = newRouter.matcher // reset router
    }
    
    export default router

    2.从后台构建menu菜单

    @RequestMapping("/get")
        @ResponseBody
        public JSONArray menu(HttpServletResponse response) throws IOException {
            StringBuilder sb=new StringBuilder();
            sb.append("[");
            sb.append("{");
            sb.append("    "path": "/login",");
            sb.append("        "hidden": true");
            sb.append("},");
    
            sb.append("{");
            sb.append("   "path": "/404",");
            sb.append("        "hidden": true");
            sb.append(" },");
    
            sb.append("{");
            sb.append("    "path": "/",");

    3.前端获取菜单

    export function getMenu() {
      return request({
        url: '/menu/get',
        method: 'get'
      })
    }

    4.渲染菜单

    created() {
            
            getMenu().then( res => {
                this.routes=res.data;
              debugger
            }).catch(error => {
              console.info(error)
                debugger
            })
      }

    调用API获取菜单,并复制到 routes 中。

     <sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
  • 相关阅读:
    Docker镜像仓库的搭建--> Harbor篇
    K8S入门系列之扩展组件 --> Dashboard (五)
    K8S入门系列之必备扩展组件--> coredns(四)
    K8S入门系列之集群二进制部署--> node篇(三)
    K8S入门系列之集群二进制部署--> master篇(二)
    K8S入门系列之集群yum安装-->初试篇(一)
    zabbix 4.2 的安装和设置(mysql57----centos7)
    SVN--服务端安装和设置---centos7
    Docker(二) Dockerfile 使用介绍
    二叉搜索树JavaScript实现
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/10867767.html
Copyright © 2011-2022 走看看