当前位置: 首页 > news >正文

37.(前端)菜单的展示

1.前端的左侧菜单栏展示

对于菜单栏,我们可以从element-ui官网中获取到展示的代码
在这里插入图片描述

1.1导入代码

<!-- src/compoents/Home.vue -->
<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <img src="../assets/logo.png">
                <span>电子后台管理系统</span>
            </div>
            <el-button type="primary" @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <el-aside width="200px">
        <el-menu default-active="2" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
        background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
        <el-submenu index="1">
            <template slot="title">
                <i class="el-icon-location"></i>
                <span>导航一</span>
            </template>
            <el-menu-item index="1-4-1">
                <i class="el-icon-location"></i>
                    <span>选项1</span>
                    </el-menu-item>
            </el-submenu>
        </el-menu>
            </el-aside>
            <el-main>Main</el-main>
    </el-container>
    </el-container>
</template>

<script>
export default{
    methods:{
        logout (){
            // 第一步,清除token
            window.sessionStorage.clear()
            // 第二步,跳转到登录页面
            this.$router.push('/login')
        },
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        }
    }
}
</script>

<style lang="less" scoped>
// 整个组件

.home-container{
    height: 100%;
}
// 界面顶
.el-header{
    display: flex;
    align-items: center; //居中操作
    background-color: #409EFF;
    justify-content: space-between;
    color: #fff;
    font-size: 20px;
    img{
        height: 50px;
        width: 100px;
    }
    div{
        display: flex;
        align-items: center;
    }
}
// 侧面
.el-aside{
    background-color: #303133;
}
// 中间
.el-main{
    background-color: #e4e7ed;
}
</style>

1.2引入相应的组件

// src/plugin/elements
import Vue from 'vue'
import { Button, FormItem, Form, Input, Message, Header, Container, Aside, Main } from 'element-ui'
import { Menu, Submenu, MenuItemGroup, MenuItem } from 'element-ui'
// import { Button } from 'element-ui'
Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Header)
Vue.use(Container)
Vue.use(Aside)
Vue.use(Main)

Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItemGroup)
Vue.use(MenuItem)
Vue.prototype.$msg = Message

效果展示
在这里插入图片描述

2.从后端填充菜单相对应的数据

在这里插入图片描述

通过使用v-for循环,遍历出当前目录下的数据,还有其子目录下的数据填充进去即可。

<!-- src/compoents/Home.vue -->
<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <img src="../assets/logo.png">
                <span>电子后台管理系统</span>
            </div>
            <el-button type="primary" @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <el-aside width="200px">
        <el-menu default-active="2" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
        background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
        <!-- 遍历传递数据给导航 -->
        <el-submenu index="1" v-for=" item in menuList" :key="item.id">
            <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{item.name}}</span>
            </template>
            <el-menu-item index="1-4-1" v-for="subItem in item.children" :key="subItem.id">
                <i class="el-icon-location"></i>
                    <span>{{subItem.name}}</span>
                    </el-menu-item>
            </el-submenu>
        </el-menu>
            </el-aside>
            <el-main>Main</el-main>
    </el-container>
    </el-container>
</template>

<script>
export default{
    // 存储导航数据
    data () {
        return {
            menuList: []
        }
    },
    // 创建时被执行的函数
    created () {
        this.getMenulist()
        console.log(this.menuList);
    },
    methods:{
        logout (){
            // 第一步,清除token
            window.sessionStorage.clear()
            // 第二步,跳转到登录页面
            this.$router.push('/login')
        },
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        // 从后端中获取数据
        // 由于我们想在他刷新页面时候就能够显示出来,所以得改为同步操作;不改的话就可能会找不到
        async getMenulist () {
            const {data: res} = await this.$axios.get('/api/menu')
            // console.log(res.data);
            this.menuList = res.data
            // console.log(res.data);
        }
    }
}
</script>

<style lang="less" scoped>
// 整个组件

.home-container{
    height: 100%;
}
// 界面顶
.el-header{
    display: flex;
    align-items: center; //居中操作
    background-color: #409EFF;
    justify-content: space-between;
    color: #fff;
    font-size: 20px;
    img{
        height: 50px;
        width: 100px;
    }
    div{
        display: flex;
        align-items: center;
    }
}
// 侧面
.el-aside{
    background-color: #303133;
}
// 中间
.el-main{
    background-color: #e4e7ed;
}
</style>

相关文章:

  • 计算机的硬件(计算机组成原理)
  • Linux权限理解
  • 注册商标的重要性
  • 65.【Study_kuang 多线程】
  • 【JavaScript】事件高级
  • STM32使用PWM+DMA方式驱动WS2812灯珠
  • 【vue】html页面怎么设置页签图标?比如B站是个小电视的图标的效果
  • 配置Swagger2生成API接口文档
  • Ai-WB1系列 AT指令连接MQTT服务器
  • 国货之光!ATECLOUD—功能如此强大的测试测量上位机开发工具软件!
  • 【SpringBoot】71、SpringBoot中集成多数据源+动态数据源
  • Python美化输出 pprint
  • Java实现十进制与二进制互相转换
  • 改进YOLOv5系列:首发结合最新Extended efficient Layer Aggregation Networks结构,高效的聚合网络设计,提升性能
  • linux:vi和vim的使用
  • es6(二):字符串的扩展
  • Git学习与使用心得(1)—— 初始化
  • go append函数以及写入
  • HomeBrew常规使用教程
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • LeetCode18.四数之和 JavaScript
  • nginx 配置多 域名 + 多 https
  • PAT A1050
  • PHP变量
  • Vue 动态创建 component
  • vue脚手架vue-cli
  • 简单数学运算程序(不定期更新)
  • 模型微调
  • 如何在 Tornado 中实现 Middleware
  • 三分钟教你同步 Visual Studio Code 设置
  • 微信开放平台全网发布【失败】的几点排查方法
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 小程序测试方案初探
  • 延迟脚本的方式
  • 用 Swift 编写面向协议的视图
  • 白色的风信子
  • #《AI中文版》V3 第 1 章 概述
  • #1014 : Trie树
  • #我与虚拟机的故事#连载20:周志明虚拟机第 3 版:到底值不值得买?
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (06)Hive——正则表达式
  • (6) 深入探索Python-Pandas库的核心数据结构:DataFrame全面解析
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (ZT)出版业改革:该死的死,该生的生
  • (函数)颠倒字符串顺序(C语言)
  • (回溯) LeetCode 46. 全排列
  • (十)Flink Table API 和 SQL 基本概念
  • (十一)图像的罗伯特梯度锐化
  • (详细文档!)javaswing图书管理系统+mysql数据库
  • (译) 函数式 JS #1:简介
  • (转)程序员技术练级攻略
  • .gitignore文件—git忽略文件
  • .htaccess配置重写url引擎
  • .Net - 类的介绍
  • .NET编程——利用C#调用海康机器人工业相机SDK实现回调取图与软触发取图【含免费源码】