当前位置:   article > 正文

vue2中左侧菜单和头部tab标签联动_vue头部菜单和左侧菜单联动

vue头部菜单和左侧菜单联动

效果图

我这里是使用的vue2 + element-ui来实现的,代码可以直接拿来使用

一、首先先安装element-ui

element-ui官网

npm i element-ui -S

 然后在main.js里面配置,安装官网的步骤来就可以了,这里就不一一介绍了

  1. import Vue from 'vue';
  2. import ElementUI from 'element-ui';
  3. import 'element-ui/lib/theme-chalk/index.css';
  4. import App from './App.vue';
  5. Vue.use(ElementUI);
  6. new Vue({
  7. el: '#app',
  8. render: h => h(App)
  9. });

二、vue页面 

home页面 

这个页面也就是主页,这里我把左侧菜单,中间内容的头部tab标签页给拆成了两个子组件他们分别是leftMenu.vue和multipleTabs.vue页面

  1. <template>
  2. <el-container>
  3. <el-aside width="collapse ? 200px : 70px">
  4. <el-button color="#626aef" @click="collapseToggle()">
  5. <el-icon>
  6. <Expand v-if="collapse" />
  7. <Fold v-else />
  8. </el-icon>
  9. </el-button>
  10. <el-menu
  11. :collapse="collapse"
  12. :default-active="store.bbc"
  13. class="el-menu-vertical-demo"
  14. unique-opened
  15. active-text-color="#ffd04b"
  16. text-color="#fff"
  17. background-color="transparent"
  18. @select="store.vv"
  19. >
  20. <left :dataToSon="store.mm" />
  21. </el-menu>
  22. </el-aside>
  23. <el-container>
  24. <el-header height="80px">
  25. <h1 @click="fff">大可的管理系统 - v1.0</h1>
  26. <div>
  27. <img src="@/assets/111.jpg" alt="">
  28. <span></span>
  29. <el-button type="primary" @click="LogOut">退出登录</el-button>
  30. </div>
  31. </el-header>
  32. <el-main>
  33. <tab></tab>
  34. </el-main>
  35. <el-footer height="50px">
  36. <p>&copy; 版权所有: 大可</p>
  37. </el-footer>
  38. </el-container>
  39. </el-container>
  40. </template>
  41. <script setup lang="ts">
  42. import { ref } from 'vue';
  43. import { useRouter } from "vue-router";
  44. import left from "../left.vue";
  45. import tab from '../tab.vue';
  46. import { ElMessage, ElMessageBox} from "element-plus";
  47. import { useAuthStore } from '@/store';
  48. import preventBack from 'vue-prevent-browser-back';//组件内单独引入
  49. const mixins = [preventBack];
  50. const store = useAuthStore();
  51. const collapse = ref<boolean>(false)
  52. const router = useRouter();
  53. const tiao = () => {
  54. console.log('路由')
  55. router.push('/son1')
  56. }
  57. const fff = () => {
  58. router.replace('/son2')
  59. }
  60. const collapseToggle = () => {
  61. collapse.value = !collapse.value
  62. }
  63. const ggvv = ref([1,2,3])
  64. const handleOpen = () => {
  65. console.log()
  66. }
  67. const gg = (e) => {
  68. console.log(e)
  69. }
  70. const handleClose = () => {
  71. console.log()
  72. }
  73. const LogOut = () => {
  74. ElMessageBox.confirm(
  75. '确定要退出登录?',
  76. 'Warning',
  77. {
  78. confirmButtonText: '确定',
  79. cancelButtonText: '取消',
  80. type: 'warning',
  81. }
  82. )
  83. .then(() => {
  84. router.replace('/login')
  85. ElMessage({
  86. type: 'success',
  87. message: '退出成功',
  88. })
  89. })
  90. .catch(() => {
  91. ElMessage({
  92. type: 'info',
  93. message: '您取消了退出',
  94. })
  95. })
  96. }
  97. </script>
  98. <style scoped>
  99. .el-header {
  100. background: url("@/assets/111.jpg");
  101. background-color: #f3d19e;
  102. display: flex;
  103. align-items: center;
  104. justify-content: space-between;
  105. }
  106. .el-header h1 {
  107. font-size: 26px;
  108. color: #fff;
  109. letter-spacing: 10px;
  110. }
  111. .el-header div {
  112. margin-right: 30px;
  113. }
  114. .el-header img {
  115. width: 40px;
  116. border-radius: 40px;
  117. vertical-align: middle;
  118. margin-right: 10px;
  119. }
  120. .el-header span {
  121. font-size: 18px;
  122. color: #fff;
  123. margin-right: 10px;
  124. }
  125. .el-header el-button {
  126. margin-left: 10px;
  127. }
  128. .el-aside {
  129. height: 100vh;
  130. background: url('@/assets/111.jpg');
  131. transition: width 0.3s;
  132. text-align: right;
  133. }
  134. .el-aside .el-button {
  135. margin: 20px 10px 20px 0;
  136. }
  137. .el-aside .el-menu {
  138. border-right: none;
  139. }
  140. .el-footer {
  141. background-color: #EBEEF5;
  142. display: flex;
  143. align-items: center;
  144. }
  145. .el-footer p {
  146. font-size: 12px;
  147. color: #666;
  148. }
  149. </style>

左侧菜单页面leftMenu.vue

  1. <template>
  2. <div>
  3. <fragment v-for="(item, index) in list" :key="index">
  4. <!-- 非叶子节点 -->
  5. <el-submenu v-if="item.children" :index="item.path">
  6. <template slot="title">
  7. <i class="el-icon-location"></i>
  8. <span class="menu" slot="title">{{ item.name }}</span>
  9. </template>
  10. <left-menu :dataToSon="item.children"></left-menu>
  11. </el-submenu>
  12. <!-- 叶子节点(功能节点) -->
  13. <el-menu-item v-else :index="item.path">
  14. <i class="el-icon-menu"></i>
  15. <span class="menu" slot="title">{{ item.name }}</span>
  16. </el-menu-item>
  17. </fragment>
  18. </div>
  19. </template>
  20. <script>
  21. import leftMenu from '../../../src/views/home/leftMenu.vue'
  22. export default {
  23. // eslint-disable-next-line vue/multi-word-component-names
  24. name: 'leftMenu',
  25. components: {
  26. leftMenu
  27. },
  28. props: ['dataToSon'],
  29. data () {
  30. return {
  31. isCollapse: false,
  32. list: []
  33. }
  34. },
  35. mounted () {
  36. this.list = this.dataToSon
  37. },
  38. methods: {
  39. handleOpen (key, keyPath) {
  40. console.log(key, keyPath)
  41. },
  42. handleClose (key, keyPath) {
  43. console.log(key, keyPath)
  44. }
  45. }
  46. }
  47. </script>
  48. <style scoped>
  49. </style>

头部tab标签页multipleTabs.vue

  1. <template>
  2. <div>
  3. <el-tabs v-model="activeTab" type="card" closable >
  4. <el-tab-pane
  5. v-for="item in this.$store.state.tabOne"
  6. :key="item.id"
  7. :label="item.name"
  8. :name="item.path"
  9. >
  10. {{item.content}}
  11. </el-tab-pane>
  12. </el-tabs>
  13. </div>
  14. </template>
  15. <script>
  16. import { mapState, mapMutations } from 'vuex'
  17. export default {
  18. name: 'MultipleTabs',
  19. computed: {
  20. ...mapState({
  21. vuexActiveTab: 'title'
  22. }),
  23. activeTab: {
  24. get () {
  25. return this.vuexActiveTab
  26. },
  27. set (newValue) {
  28. this.handleTabChange(newValue)
  29. }
  30. }
  31. },
  32. data () {
  33. return {
  34. editableTabsValue: this.$store.state.title
  35. }
  36. },
  37. mounted () {
  38. console.log(this.$store.state.title)
  39. },
  40. methods: {
  41. ...mapMutations(['SET_ACTIVE_TAB']),
  42. handleTabChange (tab) {
  43. this.SET_ACTIVE_TAB(tab)
  44. }
  45. }
  46. }
  47. </script>
  48. <style scoped>
  49. </style>

最后就是vuex里面的代码了

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. export default new Vuex.Store({
  5. state: {
  6. tree: [
  7. {
  8. path: 'vv',
  9. name: '首页',
  10. func_fid: 0,
  11. id: '1000',
  12. children: [
  13. {
  14. path: 'sy',
  15. name: '首页儿子',
  16. func_fid: 1000,
  17. id: '1212'
  18. }
  19. ]
  20. },
  21. {
  22. path: 'hh',
  23. name: '系统管理',
  24. func_fid: 0,
  25. id: '1',
  26. children: [
  27. {
  28. id: '1-1',
  29. func_fid: 1,
  30. path: 'son1',
  31. name: '系统管理儿子'
  32. },
  33. {
  34. id: '2',
  35. func_fid: 1,
  36. path: 'hhh',
  37. name: '系统管理-角色',
  38. children: [
  39. {
  40. id: '222',
  41. func_fid: 1,
  42. path: 'son1-1-1',
  43. name: '角色管理',
  44. children: [
  45. {
  46. id: '12',
  47. func_fid: 2,
  48. path: 'son1-1-1',
  49. name: '角色管理儿子',
  50. children: [
  51. {
  52. id: '122',
  53. func_fid: 2,
  54. path: 'son1-1-1-1',
  55. name: '角色管理儿子-----孙子'
  56. }
  57. ]
  58. }
  59. ]
  60. }
  61. ]
  62. },
  63. {
  64. id: '122',
  65. path: 'son1-2',
  66. name: '用户管理'
  67. }
  68. ]
  69. },
  70. {
  71. path: 'ss',
  72. name: '教学管理',
  73. id: '22',
  74. func_fid: 0,
  75. children: [
  76. {
  77. path: 'son2',
  78. name: '教学管理儿子',
  79. id: '202',
  80. func_fid: 22
  81. }
  82. ]
  83. },
  84. {
  85. path: 'zz',
  86. name: '行政管理',
  87. id: '3',
  88. func_fid: 0,
  89. children: [
  90. {
  91. path: 'son3',
  92. name: '行政管理儿子',
  93. id: '33',
  94. func_fid: 3
  95. }
  96. ]
  97. },
  98. {
  99. path: 'cv',
  100. name: '测试管理',
  101. id: '4',
  102. func_fid: 0,
  103. children: []
  104. }
  105. ],
  106. tabs: [],
  107. tabOne: [],
  108. title: 'vv'
  109. },
  110. getters: {
  111. },
  112. mutations: {
  113. tab (state, payload) {
  114. function recursion (t) {
  115. state.tabs.push(t)
  116. if (t.children && t.children.length > 0) {
  117. for (let i = 0; i < t.children.length; i++) {
  118. recursion(t.children[i])
  119. }
  120. }
  121. }
  122. state.tree.forEach(item => {
  123. recursion(item)
  124. })
  125. state.tabs.forEach(item => {
  126. if (item.path === payload) {
  127. const ind = state.tabOne.findIndex(i => i === item)
  128. state.title = item.path
  129. if (ind === -1) {
  130. state.tabOne.push(item)
  131. }
  132. }
  133. })
  134. console.log(state.tabOne)
  135. },
  136. SET_ACTIVE_TAB (state, newValue) {
  137. state.title = newValue
  138. }
  139. },
  140. actions: {
  141. },
  142. modules: {
  143. }
  144. })

到此左侧菜单和头部tab标签联动就已经完成了

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

闽ICP备14008679号