vue+element實現(xiàn)頂部導航欄+左側導航欄,選擇側邊導航欄時,頂部導航欄處于高亮

直接上效果圖,頂部導航選擇自動化測試,出現(xiàn)左側導航,左側導航選擇新建任務,這時候自動化測試也處于高亮狀態(tài),并且刷新也會仍然選中,并且當我們打開自動化測試時,默認是選中第一個的,這時候我們可以設置默認子路由

image.png

一、 首先配置路由列表

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login',
      component: login
    },
    {
      path: '/index',
      name: 'index',
      component: index,
      children: [
        {
          path: '/createPackage',
          name: 'createPackage',
          component: createPackage,
          meta: {
            activeMenu: '/createPackage'
          },
        },
        {
          path: '/packageList/:config_id',
          name: 'packageList',
          component: packageList,
          meta: {
            activeMenu: '/packageList/:config_id'
          },
        },
        {
          path: '/tagmanage',
          name: 'tagmanage',
          component: tagmanage,
          meta: {
            activeMenu: '/tagmanage'
          },
        },
        {
          path: '/tools',
          name: 'tools',
          component: tools,
          meta: {
            activeMenu: '/tools'
          },
        },
        {
          path: '/antomated-testing',
          name: 'antomated-testing',
          component: layout,
 redirect:'/antomated-testing/publish-task',
          meta: {
            activeMenu: '/antomated-testing'
          },
          children: [
            {
              path: '/antomated-testing/publish-task',
              name: 'antomated-publish-task',
              component: publishTask,
              meta: {
                activeMenu: '/antomated-testing', // 頂部導航 高亮
                ChildrenActiveMenu: '/antomated-testing/publish-task' // 子導航高亮
              }
            },
            {
              path: '/antomated-testing/task-list',
              name: 'antomated-task-list',
              component: taskList,
              meta: {
                activeMenu: '/antomated-testing', // 頂部導航 高亮
                ChildrenActiveMenu: '/antomated-testing/task-list' // 子導航高亮
              }
            },
          ]
        }
      ]
    },
  ]
})
二、配置頂部導航
 <el-menu
          :default-active="$route.meta.activeMenu"
          class="el-menu-demo"
          mode="horizontal"
          @select="handleSelect"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b">
 <el-menu-item index="/antomated-testing">
         <router-link to="/antomated-testing" style="display:block;height:100%;">自動化測試</router-link>
</el-menu-item>
</el-menu>
三、配置左側導航欄,這邊就是主要用computed檢測這個數(shù)據(jù),左側導航欄我使用了遞歸動態(tài)生成導航,不懂得可以看看我另一片文章http://www.itdecent.cn/p/6b34abeb6db7
 <el-menu
        router
        :default-active="activeMenu"
        class="el-menu-vertical-demo"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
        <Menu :menuList="LeftMenus"/>
</el-menu>

<script>
  import Menu from './menu'
  import menuList from "../../../config/menuConfig";

  export default {
    components: {Menu},
    name: "Layout",
    data() {
      return {
        LeftMenus: menuList
      }
    },
    computed: {
      activeMenu() {
        const route = this.$route
        console.log(route)
        const { meta, path } = route
        // if set path, the sidebar will highlight the path you set
        if (meta.ChildrenActiveMenu) { // 注意這里很重要
          return meta.ChildrenActiveMenu
        }
        return path
      }
    },
  }
</script>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容