• Vu3+Element-Plus根据路由配置生成菜单导航栏


    先看效果,整体界面结构如下

    image

    点击左侧菜单栏,右侧切换显示不同页面内容。

    Vue3使用路由–南河小站

    1 路由配置

    路由配置如下:

    const routes = [
    {
    path: "",
    component: () => import("@/layout/baseView.vue"),
    redirect: "/index",
    children: [
    {
    path: "/index",
    name: "首页",
    icon: "SwitchButton",
    hidden: false,
    component: () => import("@/page/dashboard/dashboard.vue"),
    },
    {
    path: "/content",
    name: "内容",
    icon: "Discount",
    hidden: false,
    component: () => import("@/layout/rightView.vue"),
    children: [
    {
    path: "manage-comment",
    icon: "MessageBox",
    name: "管理评论",
    hidden: false,
    component: () => import("@/page/content/manageComment.vue"),
    },
    {
    path: "manage-image",
    icon: "Odometer",
    name: "管理图片",
    hidden: false,
    component: () => import("@/page/content/manageImage.vue"),
    },
    ],
    },
    {
    path: "/user",
    icon: "UserFilled",
    name: "用户",
    hidden: false,
    component: () => import("@/layout/rightView.vue"),
    children: [
    {
    path: "list",
    icon: "User",
    name: "用户列表",
    hidden: false,
    component: () => import("@/page/user/list.vue"),
    },
    {
    path: "reset-pwd",
    icon: "Unlock",
    name: "重置密码",
    hidden: false,
    component: () => import("@/page/user/resetPwd.vue"),
    },
    // ....
    ],
    },
    {
    path: "/operation",
    icon: "Operation",
    name: "运维",
    hidden: false,
    component: () => import("@/layout/rightView.vue"),
    children: [
    {
    path: "mange-category",
    icon: "Edit",
    hidden: false,
    name: "管理分类",
    component: () => import("@/page/operation/manageCategory.vue"),
    },
    {
    path: "mange-carousel",
    icon: "Crop",
    name: "管理轮播图",
    hidden: false,
    component: () => import("@/page/operation/manageCarousel.vue"),
    },
    ],
    },
    ],
    },
    {
    path: "/login",
    hidden: true,
    component: () => import("@/page/login/login.vue"),
    },
    ];

    说明:

    @/layout/baseView.vue是整体页面结构

    @/layout/rightView.vue是公共页面用于显示数据内容。

    @/page/login/login.vue是登陆页面

    2 页面结构

    右侧数据内容视图是动态的,其它整个页面结构是固定的,因此提取baseView.vue作为页面基本结构。

    登录界面是另一个页面整体,因此login.vue和基本结构页面baseView.vue都在App.vue页面中通过路由进行切换,因此App.vue中添加router-view进行动态路由渲染。

    <template>
    <div id="app">
    <router-view>router-view>
    div>
    template>

    左侧菜单导航,菜单是根据路由进行动态渲染的,所以将路由生成菜单抽取为独立组组件leftMenuBar.vue。在mounted()中获取路由配置

    export default {
    data() {
    return {
    menuList: [],
    };
    },
    mounted() {
    let routes = router.options.routes;
    this.menuList = routes[0].children;
    console.log(this.menuList);
    },
    };

    ⚠️Vue3通过router.options.routes 获取配置的路由

    在右侧数据视图页面rightView.vue添加router-view标签

    <template>
    <div class="right-view">
    <div class="header">div>
    <div class="content">
    <router-view>router-view>
    div>
    div>
    template>

    3 结合Element-plus 生成菜单导航

    导入Element-plus

    遍历路由通过element-plusMenu组件生成菜单导航,完整的代码如下:

    <template>
    <div>
    <el-menu
    default-active="0"
    :unique-opened="true"
    class="el-menu-vertical-demo"
    >
    <template v-for="(item, index) in menuList" :key="index">
    <router-link :to="item.path" v-if="!item.children" :key="index">
    <el-menu-item :index="index + ''">
    <el-icon><component :is="item.icon">component>el-icon>
    <span>{{ item.name }}span>
    el-menu-item>
    router-link>
    <el-sub-menu :index="index + ''" v-else>
    <template #title>
    <el-icon><component :is="item.icon">component>el-icon>
    <span>{{ item.name }}span>
    template>
    <router-link
    :to="item.path + '/' + sub.path"
    v-for="(sub, subIndex) in item.children"
    :key="subIndex"
    >
    <el-menu-item :index="index + '-' + subIndex">
    <el-icon><component :is="sub.icon">component>el-icon>
    <span>{{ sub.name }}span>
    el-menu-item>
    router-link>
    el-sub-menu>
    template>
    el-menu>
    div>
    template>

    4 设置菜单图标

    由于element-plus使用svg 图标,复制的代码是这样的,因此在遍历路由时,就不能通过设置了,要通过来设置,:is绑定的是icon的名称

    <el-icon><component :is="item.icon">component>el-icon>

    image

    ⚠️这个地方element不同,element使用的是font-class的图标,可用 直接绑定

  • 相关阅读:
    在线XML转TSV工具
    时间序列预测(1) — 时间序列预测研究综述
    easyui disabled 属性设置
    Spring Cloud项目合规性注册之-(单元集成测试报告)
    C#使用Objects Comparer进行对象比较
    计算机网络—交换机综合实验
    【JavaEE】MyBatis
    Python封装一个接收UDP组播的模块
    【QT开发(5)】0919-QT里面新增ui类,新增使用opencv读取图片的普通类,在ui类中显示图片
    MacOS安装反编译工具JD-GUI 版本需要1.8+
  • 原文地址:https://www.cnblogs.com/ycit/p/17624261.html