当前位置:   article > 正文

ElementUI之首页导航及左侧菜单(模拟实现)_element ui菜单导航

element ui菜单导航

目录

​编辑

前言

一、mockjs简介

1. 什么是mockjs

2. mockjs的用途

3. 运用mockjs的优势

二、安装与配置mockjs

1. 安装mockjs

2. 引入mockjs

2.1 dev.env.js

2.2  prod.env.js

2.3 main.js

三、mockjs的使用

1. 将资源中的mock文件夹复制到src目录下

2. 点击登陆进行模拟测试

四、总线的搭建

1. 案例图演示

2. 创建组件

AppMain.vue

LeftNav.vue

TopNav.vue

3. 配置路由与组件的关系

index.js

4. 实现登陆跳转主页

5. 最终代码

AppMain.vue

LeftNav.vue

TopNav.vue

main.js

最终演示效果


前言

        在之前的博客中与各位老铁分享一些有关Vue的知识,想必也给大家带来了一帮助与收获,例如:ElementUI实现登陆注册、vue-cli搭建SPA项目、路由等等。今天与各位老铁带来的是Element来搭建页面首页的导航及左侧的菜单模块。

一、mockjs简介

1. 什么是mockjs

        MockJS 是一款前端开发中常用的模拟数据生成工具,用于在开发过程中模拟接口返回数据。它可以帮助前端开发人员独立于后端接口进行开发和调试,提高开发效率。MockJS 提供了一套简单却强大的 API,可以根据指定的规则生成随机的模拟数据。通过 MockJS,开发者可以定义接口的返回结构、数据类型以及各种约束条件,例如字符串长度、数字范围、日期格式等,从而模拟真实的接口返回结果。

        MockJS 的使用非常灵活,可以配合其他前端框架(如 Vue、React、Angular)或纯 JavaScript 进行集成。它还支持在浏览器环境和 Node.js 服务器环境中运行,适用于各种场景下的模拟数据需求。

        通过使用 MockJS,开发者可以快速搭建前端开发环境,独立地进行开发和调试,在后端接口未完成或不可用的情况下,仍然能够保持项目的正常进行。同时,MockJS 还可以用于编写自动化测试用例,方便进行接口测试和调试工作。

2. mockjs的用途

        Mock.js 是一个模拟数据生成器,可以帮助前端开发者快速生成模拟数据,方便进行前端开发和前后端分离开发。具体的应用场景有:

  1. 前端开发时,可以用 Mock.js 生成模拟的接口数据,方便前端进行功能开发和调试,而不依赖后端接口的实现。

  2. 前后端分离开发时,前端可以使用 Mock.js 提供的模拟数据生成功能,随时随地进行开发和测试。当后端接口完成后,只需更改配置,将 Mock.js 替换为后端接口的请求即可。

  3. 在线教育平台等需要大量测试数据的应用场景中,可以使用 Mock.js 生成大量的随机数据,用于测试和演示。

  4. 在项目初期,当后端接口还未开发完成时,可以使用 Mock.js 生成模拟数据,给前端项目提供一些数据展示效果。

3. 运用mockjs的优势

优势主要包括:

  1. 数据类型丰富:MockJS支持生成随机的文本、数字、布尔值、日期、邮箱、链接、图片、颜色等,这有助于在开发过程中模拟各种数据情况,帮助开发者更好地进行测试和调试。
  2. 拦截Ajax请求:MockJS可以在不修改既有代码的情况下,拦截Ajax请求并返回模拟的响应数据,这对于在开发过程中模拟服务器响应或进行本地测试非常有用。
  3. 前后端分离:MockJS可以帮助前端独立于后端进行开发,这对于前后端分离的开发模式非常有益,可以加快开发进度和提高效率。
  4. 单元测试:MockJS的随机数据模拟功能可以帮助开发者在单元测试中增加测试的真实性,通过模拟各种场景来提高测试的完整性和可靠性。
  5. 无侵入性:MockJS不需要修改既有代码就可以进行拦截和模拟,这对于不希望修改生产代码的场景非常适用,比如在进行前端开发时进行接口测试。
  6. 简单易用:MockJS的接口符合直觉,易于使用,使得开发者可以快速上手并开始使用。
  7. 方便扩展:MockJS支持扩展更多数据类型,支持自定义函数和正则表达式,这使得MockJS可以满足更多的开发需求。二、

二、安装与配置mockjs

1. 安装mockjs

在项目的目录下进入cmd终端窗口,输入一下指令进行安装。

npm i mockjs -D

注:-D表示只在开发环境中使用

2. 引入mockjs

2.1 dev.env.js

  1. module.exports = merge(prodEnv, {
  2. NODE_ENV: '"development"',
  3. MOCK: 'true'
  4. })

2.2  prod.env.js

  1. module.exports = {
  2. NODE_ENV: '"production"',
  3. MOCK: 'false'
  4. }
'
运行

2.3 main.js

在动态main.js中加载mockjs,在开发环境下才会使用,生产环境不会使用。

  1. //开发环境下才会引入mockjs
  2. process.env.MOCK && require('@/mock')
'
运行

三、mockjs的使用

1. 将资源中的mock文件夹复制到src目录下

 该文件下的json文件夹中的login-mock.js是用来创造模拟数据的,index.js是用来将数据与登陆请求进行绑定的。

2. 点击登陆进行模拟测试

 点击多次登陆后,code可能发生改变(-1/0),但是message的长度不一。

 模拟当前登陆成功与失败,不进行后端交互

Login.vue

  1. <template>
  2. <div class="login">
  3. <template>
  4. <div class="login-wrap">
  5. <el-form class="login-container">
  6. <h1 class="title">君易官网登录</h1>
  7. <el-form-item label="">
  8. <el-input type="text" v-model="username" placeholder="登录账号" autocomplete="off"></el-input>
  9. </el-form-item>
  10. <el-form-item label="">
  11. <el-input type="password" v-model="password" placeholder="登录密码" autocomplete="off"></el-input>
  12. </el-form-item>
  13. <el-form-item>
  14. <el-button type="primary" style="width:100%;" @click="doSubmit()">提交</el-button>
  15. </el-form-item>
  16. <el-row style="text-align: center;margin-top:-10px">
  17. <el-link type="primary">忘记密码</el-link>
  18. <el-link type="primary" @click="gotoRegister()">用户注册</el-link>
  19. </el-row>
  20. </el-form>
  21. </div>
  22. </template>
  23. </div>
  24. </template>
  25. <script>
  26. // import axios from 'axios'
  27. // import qs from 'qs'
  28. export default {
  29. name: 'Login',
  30. data() {
  31. return {
  32. msg: 'Welcome to Your Vue.js App',
  33. username: '',
  34. password: ''
  35. }
  36. },
  37. methods: {
  38. gotoRegister() {
  39. // 跳转注册页面
  40. this.$router.push('/Register');
  41. },
  42. doSubmit() {
  43. // 接受参数
  44. let params = {
  45. username : this.username,
  46. password : this.password
  47. };
  48. // 打印输出查看
  49. console.log(params);
  50. // 定义请求参数的访问地址
  51. var url = this.axios.urls.SYSTEM_USER_DOLOGIN;
  52. // 想后端发送post请求
  53. this.axios.post(url, params).then(r => {
  54. // 打印
  55. console.log(r);
  56. // 输入的账号密码正确则弹出登陆成功提示,反之弹出密码或账号有误提示
  57. // if (r.data.success) {
  58. // this.$message({
  59. // message: r.data.msg,
  60. // type: 'success'
  61. // });
  62. // } else {
  63. // this.$message.error(r.data.msg);
  64. // }
  65. this.$message({
  66. message: r.data.message,
  67. type: r.data.code ==0 ? 'success' : 'error'
  68. });
  69. }).catch(e => {
  70. console.log(e);
  71. });
  72. }
  73. }
  74. }
  75. </script>
  76. <style scoped>
  77. .login-wrap {
  78. box-sizing: border-box;
  79. width: 100%;
  80. height: 100%;
  81. padding-top: 10%;
  82. background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+Cjxzdmcgd2lkdGg9IjEzNjFweCIgaGVpZ2h0PSI2MDlweCIgdmlld0JveD0iMCAwIDEzNjEgNjA5IiB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgogICAgPCEtLSBHZW5lcmF0b3I6IFNrZXRjaCA0Ni4yICg0NDQ5NikgLSBodHRwOi8vd3d3LmJvaGVtaWFuY29kaW5nLmNvbS9za2V0Y2ggLS0+CiAgICA8dGl0bGU+R3JvdXAgMjE8L3RpdGxlPgogICAgPGRlc2M+Q3JlYXRlZCB3aXRoIFNrZXRjaC48L2Rlc2M+CiAgICA8ZGVmcz48L2RlZnM+CiAgICA8ZyBpZD0iQW50LURlc2lnbi1Qcm8tMy4wIiBzdHJva2U9Im5vbmUiIHN0cm9rZS13aWR0aD0iMSIgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj4KICAgICAgICA8ZyBpZD0i6LSm5oi35a+G56CB55m75b2VLeagoemqjCIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTc5LjAwMDAwMCwgLTgyLjAwMDAwMCkiPgogICAgICAgICAgICA8ZyBpZD0iR3JvdXAtMjEiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDc3LjAwMDAwMCwgNzMuMDAwMDAwKSI+CiAgICAgICAgICAgICAgICA8ZyBpZD0iR3JvdXAt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ERUVEIiBjeD0iMjQzLjgzMzMzMyIgY3k9IjMwLjMzMzMzMzMiIHI9IjUuODMzMzMzMzMiPjwvY2lyY2xlPgogICAgICAgICAgICAgICAgICAgIDxjaXJjbGUgaWQ9Ik92YWwtNS1Db3B5LTUiIGZpbGw9IiNFMEI0QjciIGN4PSIxNzUuNTgzMzMzIiBjeT0iMjMyLjc1IiByPSI1LjI1Ij48L2NpcmNsZT4KICAgICAgICAgICAgICAgIDwvZz4KICAgICAgICAgICAgPC9nPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+);
  83. /* background-color: #112346; */
  84. background-repeat: no-repeat;
  85. background-position: center right;
  86. background-size: 100%;
  87. }
  88. .login-container {
  89. border-radius: 10px;
  90. margin: 0px auto;
  91. width: 350px;
  92. padding: 30px 35px 15px 35px;
  93. background: #fff;
  94. border: 1px solid #eaeaea;
  95. text-align: left;
  96. box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  97. }
  98. .title {
  99. margin: 0px auto 40px auto;
  100. text-align: center;
  101. color: #505458;
  102. }
  103. </style>

 运行效果

四、总线的搭建

1. 案例图演示

2. 创建组件

如案例演示图所示,我们需要创建三个组件来完成效果搭建,由于三个组件是公共部分所以复制方到components文件下,将所需图片放到assets文件下。

AppMain.vue

  1. <template>
  2. <el-container class="main-container">
  3. <el-aside v-bind:class="asideClass">
  4. <LeftNav></LeftNav>
  5. </el-aside>
  6. <el-container>
  7. <el-header class="main-header">
  8. <TopNav></TopNav>
  9. </el-header>
  10. <el-main class="main-center">Main</el-main>
  11. </el-container>
  12. </el-container>
  13. </template>
  14. <script>
  15. // 导入组件
  16. import TopNav from '@/components/TopNav.vue'
  17. import LeftNav from '@/components/LeftNav.vue'
  18. // 导出模块
  19. export default {
  20. };
  21. </script>
  22. <style scoped>
  23. .main-container {
  24. height: 100%;
  25. width: 100%;
  26. box-sizing: border-box;
  27. }
  28. .main-aside-collapsed {
  29. /* 在CSS中,通过对某一样式声明! important ,可以更改默认的CSS样式优先级规则,使该条样式属性声明具有最高优先级 */
  30. width: 64px !important;
  31. height: 100%;
  32. background-color: #334157;
  33. margin: 0px;
  34. }
  35. .main-aside {
  36. width: 240px !important;
  37. height: 100%;
  38. background-color: #334157;
  39. margin: 0px;
  40. }
  41. .main-header,
  42. .main-center {
  43. padding: 0px;
  44. border-left: 2px solid #333;
  45. }
  46. </style>

LeftNav.vue

  1. <template>
  2. <el-menu default-active="2" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose" background-color="#334157"
  3. text-color="#fff" active-text-color="#ffd04b" :collapse="collapsed" :collapse-transition="bb">
  4. <!-- <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b"> -->
  5. <div class="logobox">
  6. <img class="logoimg" src="../assets/img/logo.png" alt="">
  7. </div>
  8. <el-submenu index="1">
  9. <template slot="title">
  10. <i class="el-icon-location"></i>
  11. <span>导航一</span>
  12. </template>
  13. <el-menu-item-group>
  14. <template slot="title">分组一</template>
  15. <el-menu-item index="1-1">选项1</el-menu-item>
  16. <el-menu-item index="1-2">选项2</el-menu-item>
  17. </el-menu-item-group>
  18. <el-menu-item-group title="分组2">
  19. <el-menu-item index="1-3">选项3</el-menu-item>
  20. </el-menu-item-group>
  21. <el-submenu index="1-4">
  22. <template slot="title">选项4</template>
  23. <el-menu-item index="1-4-1">选项1</el-menu-item>
  24. </el-submenu>
  25. </el-submenu>
  26. <el-menu-item index="2">
  27. <i class="el-icon-menu"></i>
  28. <span slot="title">导航二</span>
  29. </el-menu-item>
  30. <el-menu-item index="3" disabled>
  31. <i class="el-icon-document"></i>
  32. <span slot="title">导航三</span>
  33. </el-menu-item>
  34. <el-menu-item index="4">
  35. <i class="el-icon-setting"></i>
  36. <span slot="title">导航四</span>
  37. </el-menu-item>
  38. </el-menu>
  39. </template>
  40. <script>
  41. export default {
  42. }
  43. </script>
  44. <style>
  45. .el-menu-vertical-demo:not(.el-menu--collapse) {
  46. width: 240px;
  47. min-height: 400px;
  48. }
  49. .el-menu-vertical-demo:not(.el-menu--collapse) {
  50. border: none;
  51. text-align: left;
  52. }
  53. .el-menu-item-group__title {
  54. padding: 0px;
  55. }
  56. .el-menu-bg {
  57. background-color: #1f2d3d !important;
  58. }
  59. .el-menu {
  60. border: none;
  61. }
  62. .logobox {
  63. height: 40px;
  64. line-height: 40px;
  65. color: #9d9d9d;
  66. font-size: 20px;
  67. text-align: center;
  68. padding: 20px 0px;
  69. }
  70. .logoimg {
  71. height: 40px;
  72. }
  73. </style>

TopNav.vue

  1. <template>
  2. <!-- <el-menu :default-active="activeIndex2" class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64"
  3. text-color="#fff" active-text-color="#ffd04b">
  4. <el-menu-item index="1">处理中心</el-menu-item>
  5. <el-submenu index="2">
  6. <template slot="title">我的工作台</template>
  7. <el-menu-item index="2-1">选项1</el-menu-item>
  8. <el-menu-item index="2-2">选项2</el-menu-item>
  9. <el-menu-item index="2-3">选项3</el-menu-item>
  10. <el-submenu index="2-4">
  11. <template slot="title">选项4</template>
  12. <el-menu-item index="2-4-1">选项1</el-menu-item>
  13. <el-menu-item index="2-4-2">选项2</el-menu-item>
  14. <el-menu-item index="2-4-3">选项3</el-menu-item>
  15. </el-submenu>
  16. </el-submenu>
  17. <el-menu-item index="3" disabled>消息中心</el-menu-item>
  18. <el-menu-item index="4"><a href="https://www.ele.me" target="_blank">订单管理</a></el-menu-item>
  19. </el-menu> -->
  20. <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
  21. <el-button class="buttonimg">
  22. <img class="showimg" :src="collapsed?imgshow:imgsq" @click="doToggle()">
  23. </el-button>
  24. <el-submenu index="2" class="submenu">
  25. <template slot="title">超级管理员</template>
  26. <el-menu-item index="2-1">设置</el-menu-item>
  27. <el-menu-item index="2-2">个人中心</el-menu-item>
  28. <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
  29. </el-submenu>
  30. </el-menu>
  31. </template>
  32. <script>
  33. export default {
  34. }
  35. </script>
  36. <style scoped>
  37. .el-menu-vertical-demo:not(.el-menu--collapse) {
  38. border: none;
  39. }
  40. .submenu {
  41. float: right;
  42. }
  43. .buttonimg {
  44. height: 60px;
  45. background-color: transparent;
  46. border: none;
  47. }
  48. .showimg {
  49. width: 26px;
  50. height: 26px;
  51. position: absolute;
  52. top: 17px;
  53. left: 17px;
  54. }
  55. .showimg:active {
  56. border: none;
  57. }
  58. </style>

3. 配置路由与组件的关系

index.js

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import HelloWorld from '@/components/HelloWorld'
  4. import AppMain from '@/components/AppMain'
  5. import LeftNav from '@/components/LeftNav'
  6. import TopNav from '@/components/TopNav'
  7. import Login from '@/views/Login'
  8. import Register from '@/views/Register'
  9. Vue.use(Router)
  10. export default new Router({
  11. routes: [ //默认首页
  12. {
  13. path: '/',
  14. name: 'Login',
  15. component: Login
  16. }, {
  17. path: '/Register',
  18. name: 'Register',
  19. component: Register
  20. }, {
  21. path: '/Login',
  22. name: 'Login',
  23. component: Login
  24. }, {
  25. path: '/AppMain',
  26. name: 'AppMain',
  27. component: AppMain,
  28. children;
  29. [{
  30. path: '/LeftNav',
  31. name: 'LeftNav',
  32. component: LeftNav
  33. }, {
  34. path: '/TopNav',
  35. name: 'TopNav',
  36. component: TopNav
  37. }]
  38. }
  39. ]
  40. })

4. 实现登陆跳转主页

 this.$router.push('/AppMain');

 

 这只是半成品,后还需进行调整才能显示样式和效果。

5. 最终代码

AppMain.vue

  1. <template>
  2. <el-container class="main-container">
  3. <el-aside v-bind:class="asideClass">
  4. <LeftNav></LeftNav>
  5. </el-aside>
  6. <el-container>
  7. <el-header class="main-header">
  8. <TopNav></TopNav>
  9. </el-header>
  10. <el-main class="main-center">Main</el-main>
  11. </el-container>
  12. </el-container>
  13. </template>
  14. <script>
  15. // 导入组件
  16. import TopNav from '@/components/TopNav.vue'
  17. import LeftNav from '@/components/LeftNav.vue'
  18. // 导出模块
  19. export default {
  20. components: {
  21. TopNav,
  22. LeftNav
  23. },
  24. data() {
  25. return {
  26. asideClass: 'main-aside'
  27. }
  28. },created(){
  29. this.$root.Bus.$on('xxx',v=>{
  30. this.asideClass = v ? 'main-aside-collapsed' : 'main-aside';
  31. });
  32. }
  33. };
  34. </script>
  35. <style scoped>
  36. .main-container {
  37. height: 100%;
  38. width: 100%;
  39. box-sizing: border-box;
  40. }
  41. .main-aside-collapsed {
  42. /* 在CSS中,通过对某一样式声明! important ,可以更改默认的CSS样式优先级规则,使该条样式属性声明具有最高优先级 */
  43. width: 64px !important;
  44. height: 100%;
  45. background-color: #334157;
  46. margin: 0px;
  47. }
  48. .main-aside {
  49. width: 240px !important;
  50. height: 100%;
  51. background-color: #334157;
  52. margin: 0px;
  53. }
  54. .main-header,
  55. .main-center {
  56. padding: 0px;
  57. border-left: 2px solid #333;
  58. }
  59. </style>

LeftNav.vue

  1. <template>
  2. <el-menu default-active="2" class="el-menu-vertical-demo" background-color="#334157"
  3. text-color="#fff" active-text-color="#ffd04b" :collapse="collapsed" >
  4. <!-- <el-menu default-active="2" :collapse="collapsed" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo" background-color="#334157" text-color="#fff" active-text-color="#ffd04b"> -->
  5. <div class="logobox">
  6. <img class="logoimg" src="../assets/img/logo.png" alt="">
  7. </div>
  8. <el-submenu index="1">
  9. <template slot="title">
  10. <i class="el-icon-location"></i>
  11. <span>导航一</span>
  12. </template>
  13. <el-menu-item-group>
  14. <template slot="title">分组一</template>
  15. <el-menu-item index="1-1">选项1</el-menu-item>
  16. <el-menu-item index="1-2">选项2</el-menu-item>
  17. </el-menu-item-group>
  18. <el-menu-item-group title="分组2">
  19. <el-menu-item index="1-3">选项3</el-menu-item>
  20. </el-menu-item-group>
  21. <el-submenu index="1-4">
  22. <template slot="title">选项4</template>
  23. <el-menu-item index="1-4-1">选项1</el-menu-item>
  24. </el-submenu>
  25. </el-submenu>
  26. <el-menu-item index="2">
  27. <i class="el-icon-menu"></i>
  28. <span slot="title">导航二</span>
  29. </el-menu-item>
  30. <el-menu-item index="3" disabled>
  31. <i class="el-icon-document"></i>
  32. <span slot="title">导航三</span>
  33. </el-menu-item>
  34. <el-menu-item index="4">
  35. <i class="el-icon-setting"></i>
  36. <span slot="title">导航四</span>
  37. </el-menu-item>
  38. </el-menu>
  39. </template>
  40. <script>
  41. export default {
  42. data() {
  43. return{
  44. collapsed:false
  45. }
  46. },created(){
  47. this.$root.Bus.$on('xxx',v=>{
  48. this.collapsed = v;
  49. });
  50. }
  51. }
  52. </script>
  53. <style>
  54. .el-menu-vertical-demo:not(.el-menu--collapse) {
  55. width: 240px;
  56. min-height: 400px;
  57. }
  58. .el-menu-vertical-demo:not(.el-menu--collapse) {
  59. border: none;
  60. text-align: left;
  61. }
  62. .el-menu-item-group__title {
  63. padding: 0px;
  64. }
  65. .el-menu-bg {
  66. background-color: #1f2d3d !important;
  67. }
  68. .el-menu {
  69. border: none;
  70. }
  71. .logobox {
  72. height: 40px;
  73. line-height: 40px;
  74. color: #9d9d9d;
  75. font-size: 20px;
  76. text-align: center;
  77. padding: 20px 0px;
  78. }
  79. .logoimg {
  80. height: 40px;
  81. }
  82. </style>

TopNav.vue

  1. <template>
  2. <!-- <el-menu :default-active="activeIndex2" class="el-menu-demo" mode="horizontal" @select="handleSelect" background-color="#545c64"
  3. text-color="#fff" active-text-color="#ffd04b">
  4. <el-menu-item index="1">处理中心</el-menu-item>
  5. <el-submenu index="2">
  6. <template slot="title">我的工作台</template>
  7. <el-menu-item index="2-1">选项1</el-menu-item>
  8. <el-menu-item index="2-2">选项2</el-menu-item>
  9. <el-menu-item index="2-3">选项3</el-menu-item>
  10. <el-submenu index="2-4">
  11. <template slot="title">选项4</template>
  12. <el-menu-item index="2-4-1">选项1</el-menu-item>
  13. <el-menu-item index="2-4-2">选项2</el-menu-item>
  14. <el-menu-item index="2-4-3">选项3</el-menu-item>
  15. </el-submenu>
  16. </el-submenu>
  17. <el-menu-item index="3" disabled>消息中心</el-menu-item>
  18. <el-menu-item index="4"><a href="https://www.ele.me" target="_blank">订单管理</a></el-menu-item>
  19. </el-menu> -->
  20. <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
  21. <el-button class="buttonimg">
  22. <img class="showimg" :src="collapsed?imgshow:imgsq" @click="doToggle()">
  23. </el-button>
  24. <el-submenu index="2" class="submenu">
  25. <template slot="title">超级管理员</template>
  26. <el-menu-item index="2-1">设置</el-menu-item>
  27. <el-menu-item index="2-2">个人中心</el-menu-item>
  28. <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
  29. </el-submenu>
  30. </el-menu>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. collapsed: false,
  37. imgshow: require('@/assets/img/show.png'),
  38. imgsq: require('@/assets/img/sq.png')
  39. }
  40. },
  41. methods: {
  42. doToggle(){
  43. this.collapsed = !this.collapsed;
  44. // 将是否折叠的变量放入总线
  45. this.$root.Bus.$emit('xxx',this.collapsed);
  46. },
  47. exit(){
  48. this.$router.push("/");
  49. }
  50. }
  51. }
  52. </script>
  53. <style scoped>
  54. .el-menu-vertical-demo:not(.el-menu--collapse) {
  55. border: none;
  56. }
  57. .submenu {
  58. float: right;
  59. }
  60. .buttonimg {
  61. height: 60px;
  62. background-color: transparent;
  63. border: none;
  64. }
  65. .showimg {
  66. width: 26px;
  67. height: 26px;
  68. position: absolute;
  69. top: 17px;
  70. left: 17px;
  71. }
  72. .showimg:active {
  73. border: none;
  74. }
  75. </style>

main.js

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. //开发环境下才会引入mockjs
  5. process.env.MOCK && require('@/mock')
  6. // 新添加1
  7. import ElementUI from 'element-ui'
  8. // 新添加2,避免后期打包样式不同,要放在import App from './App';之前
  9. import 'element-ui/lib/theme-chalk/index.css'
  10. import App from './App'
  11. import router from './router'
  12. Vue.use(ElementUI);
  13. import axios from '@/api/http'
  14. import VueAxios from 'vue-axios'
  15. Vue.use(VueAxios,axios)
  16. Vue.config.productionTip = false
  17. /* eslint-disable no-new */
  18. new Vue({
  19. el: '#app',
  20. router,
  21. data(){
  22. return{
  23. Bus:new Vue()
  24. }
  25. },
  26. components: { App },
  27. template: '<App/>'
  28. })

最终演示效果

这就是总线的基础使用了 

         本期博客与大家分享了有关mockjs及总线的一些使用,并在项目中进行运用演示,希望大家能够三连加关注支持博主,后期会对该项目进行扩展及完善,敬请期待。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/971887
推荐阅读
相关标签
  

闽ICP备14008679号