点击侧边栏菜单跳转到main 页面

.home.vue页面

需要在 <el-main>标签下增加第一个路由占位符

因为登录的欢迎页面 和点击下的子菜单 会在同一个页面

因此需要判断 获取路由路径 如果是/home 路径 则显示欢迎语(Devops管理系统) 如果不是则是路由占位符

home.vue 中

html 复制代码
<el-main>
          <div class="mainTitle" v-if="$route.path==='/home'">Devops管理系统</div>
          <!-- 路由占位符-->
          <router-view></router-view>
</el-main>

router.ts 中定义两个 子菜单 加载这两个页面

router.ts

html 复制代码
// 登录成功之后home页路由
        path : '/home',
        name : 'home',
        component : () => import('@/views/Home.vue'),
        children: [
            {
                path: '/sys/deploy', component: ()=>import('@/sys/SysDeploy.vue'),

            },
            {
                path: '/sys/app', component: ()=>import('@/sys/SysApp.vue')
            }
        ]
        },

后端接口:

html 复制代码
[
    {
        "id": 6,
        "url": "/",
        "path": "/home",
        "component": "Home",
        "name": "系统管理",
        "iconCls": "fa fa-windows",
        "meta": {
            "keepAlive": null,
            "requireAuth": true
        },
        "
        "children": [
            {
                "id": 30,
                "path": "/sys/deploy",
                "component": "SysDeploy",
                "name": "应用部署",
                "iconCls": "fa fa-ad",
                "meta": null,
                
            },
            {
                "id": 7,
                "path": "/sys/app",
                "component": "SysApp",
                "name": "App上传",
                "iconCls": "fa-solid fa-circle-arrow-up",
                "meta": null,
                "parentId": 6,
                "enabled": true,
                "children": null,
                "roles": null
            }
        ],
    }
]

前端依赖的后端接口字段 path, component

完整代码

router.ts

html 复制代码
//以 npm方式引入 vue3 路由
//定义的默认路由
import { createRouter,createWebHashHistory} from 'vue-router'
import Ad from "@/components/Ad.vue";
//公共路由
const publicRoutes = [
        {
        //登录
        path : '/',  // 路由路径 根组件对应的登录页面 对应后端
        name : 'Login',
        component : () => import('@/views/Login.vue'),
        },
        {
        // 登录成功之后home页路由
        path : '/home',
        name : 'home',
        component : () => import('@/views/Home.vue'),
        children: [
            {
                path: '/sys/deploy', component: ()=>import('@/sys/SysDeploy.vue'),

            },
            {
                path: '/sys/app', component: ()=>import('@/sys/SysApp.vue')
            }
        ]
        },

        {
        path : '/register',
        name : 'register',
        component : () => import('@/views/Register.vue')
        },
        {
        // 404
        path : '/404',
        name : '404',
        component : () => import('@/views/404.vue')
        },
        {
        // 任意路由
        path : '/:pathMatch(.*)*',
        name : 'Any',
        redirect : '/404'
        }
]
const router = createRouter({
    history : createWebHashHistory(),      //路由的hash模式
    // 定义一个路由数组 每个路由都需要映射到一个组件
    routes  : publicRoutes,
    // 使用浏览器的回退或者前进时,重新返回时保留滚动位置,跳转页面的话,不触发
    scrollBehavior(to,from,savePosition){
        if (savePosition){
            return savePosition;
        }else {
            return {top:0}
        }
    }
});

export default router

home.vue

html 复制代码
<template>
  <div class="common-layout">
    <el-container>
      <el-header class="homeHeader">
        <div class="headerTitle">Devops平台</div>
      </el-header>
      <el-container>
        <el-aside width="400px">
          <el-row class="tac">
            <el-col :span="12">
              <el-menu
                  default-active="2"
                  class="el-menu-vertical-demo"
                  router
              >
                <el-sub-menu index="1" v-for="item in menuList" :key="item.id">
                  <template #title>
                   <i :class="item.iconCls"/>
                    <span>{{ item.name}}</span>
                  </template>
                  <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id">
                    <template #title>
                    <span>{{subItem.name}}</span>
                    </template>
                  </el-menu-item>
                </el-sub-menu>
              </el-menu>
            </el-col>
            </el-row>
        </el-aside>
        <el-main>
          <div class="mainTitle" v-if="$route.path==='/home'">Devops管理系统</div>
          <!-- 路由占位符-->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { Menu , Document, Location, Setting, Burger} from "@element-plus/icons-vue";
import axios from "axios";
import {onMounted,ref } from 'vue'
// 注册开关
const redirect = ref(undefined)
export default {
  name: "Home",
  components: {Burger, Setting, Document, Location},
  setup() {
    const menuList = ref();
    onMounted(()=>{
      axios.get("/api/menu").then(res =>{
        console.log("onMounted")
        const data = res.data
        console.log(data)
        menuList.value = data
      })
    })
    return {
      menuList
    }
  },
}
</script>



<style lang="less" scoped>

.homeHeader{
  background-color: #04befe;
  /*弹性展示*/
  display: flex;
  /* 居中对齐弹性盒子的各项 div 元素*/
  align-items: center;

}
.headerTitle {
  /* 字体大小*/
  font-size: 20px;
  /* 字体颜色*/
  color: #fffff9;
}
.mainTitle {
  /* 规定元素中文本的水平对齐方式 居中 */
  text-align: center;
  /* 颜色为深空色 */
  color: #04befe;
  /* 字体大小*/
  font-size: 30px;
/* 距离顶部的距离为 20px 数值越大下降位置越多 */
/* padding-top: 20px; */
}





</style>
相关推荐
布瑞泽的童话20 分钟前
无需切换平台?TuneFree如何搜罗所有你爱的音乐
前端·vue.js·后端·开源
白鹭凡32 分钟前
react 甘特图之旅
前端·react.js·甘特图
2401_8628867836 分钟前
蓝禾,汤臣倍健,三七互娱,得物,顺丰,快手,游卡,oppo,康冠科技,途游游戏,埃科光电25秋招内推
前端·c++·python·算法·游戏
书中自有妍如玉44 分钟前
layui时间选择器选择周 日月季度年
前端·javascript·layui
Riesenzahn44 分钟前
canvas生成图片有没有跨域问题?如果有如何解决?
前端·javascript
f8979070701 小时前
layui 可以使点击图片放大
前端·javascript·layui
忘不了情1 小时前
左键选择v-html绑定的文本内容,松开鼠标后出现复制弹窗
前端·javascript·html
码上飞扬1 小时前
前端框架对比选择:如何在众多技术中找到最适合你的
vue.js·前端框架·react·angular·svelte
世界尽头与你1 小时前
HTML常见语法设计
前端·html