zoukankan      html  css  js  c++  java
  • vue+elementui搭建后台管理界面(3侧边栏菜单)

    上一节搭好了主框架,但是标签页和侧边栏只是分别展示了各自的菜单,如何将二者联动起来?
    定义路由规则:当有 children 属性时,从 children 里取出 path 填充到侧边栏,如:

    {
      path: '/',
      redirect: '/dashboard',
      name: 'Container',
      component: Container,
      children: [
        {path: 'dashboard', name: '首页', component: Dashboard, },
        {path: 'article', name: '文章', component: Article, },
      ]
    }
    

    该路由的 children 有2个子路由,我期望生成如下菜单:

    1 新增组件

    新增 views/dashboard/index.vue

    <template>
      <h1>dashboard</h1>
    </template>
    

    views/article/index.vue

    <template>
      <h1>Article</h1>
    </template>
    

    此时 src 的目录结构

    │  App.vue
    │  main.js
    ├─assets
    │      logo.png
    ├─components
    │      HelloWorld.vue
    ├─container
    │      Container.vue
    ├─router
    │      index.js
    ├─styles
    │      index.scss
    └─views
        │  TheLogin.vue
        ├─article
        │      index.vue
        └─dashboard
                index.vue
    

    2 修改路由

    修改 src/router/index.js :

    import Vue from 'vue'
    import Router from 'vue-router'
    import Login from '@/views/TheLogin'
    import Container from '@/container/Container'
    import Dashboard from '@/views/dashboard'
    import Article from '@/views/article'
    
    Vue.use(Router)
    
    export default new Router({
      routes: [
        {
          path: '/login',
          name: 'Login',
          component: Login
        },
        {
          path: '/',
          redirect: '/dashboard',
          name: 'Container',
          component: Container,
          children: [
            {path: 'dashboard', name: '首页', component: Dashboard, },
            {path: 'article', name: '文章', component: Article, },
          ]
        }
      ]
    })
    

    3 页面主框架

    修改 src/container/Container.vue
    el-header 的样例菜单替换为

    <el-menu default-active="/"
            router
              class="el-menu-demo tab-page"
              mode="horizontal"
              @select="handleSelect"
              active-text-color="#409EFF">
      <el-menu-item index="/">首页</el-menu-item>
    </el-menu>
    

    el-aside 的样例菜单替换为

    <el-menu :default-active="defaultActive"
            router
              class="el-menu-vertical-demo"
              @open="handleOpen"
              :collapse="isCollapse">
      <template v-for="route in $router.options.routes" v-if="route.children && route.children.length">
          <template v-for="item in route.children" >
            <el-menu-item 
              :key="route.path + '/' + item.path"
              :index="item.path"
            >
              <i class="el-icon-menu"></i>
              <span slot="title">{{ item.name }}</span>
          </el-menu-item>
          </template>
      </template>
    </el-menu>
    

    此时的页面:

  • 相关阅读:
    Linux中逻辑卷的快照与还原
    Linux 中磁盘阵列RAID10损坏以及修复
    Linux 中磁盘阵列RAID10配置
    Linux 中磁盘容量配额
    Centos7VMware虚拟机最小化安装后,安装Tenda U12 USB无线网卡驱动
    安装vmware-tools遇the path "" is not valid path to the gcc binary和the path "" is not a valid path to the 3.10.0-327.e17.x86_64 kernel headers问题解决
    /etc/postfix下 main.cf 配置文件详解
    Linux安装配置vsftp搭建FTP的详细配置
    Linux中ftp的常用命令
    javascript深入理解js闭包
  • 原文地址:https://www.cnblogs.com/wbjxxzx/p/9943049.html
Copyright © 2011-2022 走看看