header.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <nav class="client-head">
  3. <div class="client-container head-layout">
  4. <div class="head-left">
  5. <a>
  6. <i></i>
  7. </a>
  8. <el-menu
  9. :default-active="activeIndex"
  10. class="el-menu-demo"
  11. mode="horizontal"
  12. :router="false"
  13. >
  14. <template v-for="(item,index) in headerArr">
  15. <el-submenu :index="`${item.keyPath}`" v-if="item.children && item.children.length > 0"
  16. popper-class="mta-menu-two">
  17. <template slot="title">{{item.content}}</template>
  18. <el-menu-item :index="`${child.keyPath}`" v-for="child of item.children"
  19. :key="child.keyPath">
  20. <mtaLink :path="child.keyPath" :instation="true" :content="child.content"></mtaLink>
  21. </el-menu-item>
  22. </el-submenu>
  23. <el-menu-item :index="`${item.keyPath}`" v-else>
  24. <mtaLink :path="item.keyPath" :instation="true" :content="item.content"></mtaLink>
  25. </el-menu-item>
  26. </template>
  27. </el-menu>
  28. </div>
  29. <div class="head-right">
  30. <i></i><span>4000-522-130</span>
  31. </div>
  32. </div>
  33. </nav>
  34. </template>
  35. <script>
  36. import mtaLink from '~/components/mtaLink/index';
  37. let _ = require('lodash');
  38. export default {
  39. name: 'mtaHeader',
  40. components: {
  41. mtaLink,
  42. },
  43. props: {
  44. activeNav: { // 默认选中的 路径 与组件中的index相互对应
  45. type: String,
  46. require: true,
  47. default: '0',
  48. },
  49. },
  50. data() {
  51. return {
  52. activeIndex: this.activeNav,
  53. headerArr: [
  54. {
  55. content: '首页',
  56. keyPath: '/?userId=1',
  57. },
  58. {
  59. content: '考试平台',
  60. keyPath: '/examsystem',
  61. children: [],
  62. },
  63. {
  64. content: '培训平台',
  65. keyPath: '/pages/trainsystem',
  66. children: [],
  67. },
  68. {
  69. content: '课程开发',
  70. keyPath: '/pages/courseDev',
  71. children: [],
  72. },
  73. {
  74. content: '新闻资讯',
  75. keyPath: '/pages/newsInfor',
  76. children: [],
  77. },
  78. {
  79. content: '帮助中心',
  80. keyPath: '/pages/helpCenter',
  81. children: [],
  82. }, {
  83. content: '关于我们',
  84. keyPath: '/pages/aboutUs',
  85. children: [],
  86. },
  87. ],
  88. };
  89. },
  90. watch: {
  91. // 监听
  92. '$route.path': {
  93. handler(newVal, oldVal) {
  94. this.activeIndexSync();
  95. },
  96. immediate: true,
  97. },
  98. /* $route: {
  99. handler: function(val, oldVal){
  100. debugger;
  101. this.activeIndexSync();
  102. },
  103. // 深度观察监听
  104. deep: true
  105. }*/
  106. },
  107. methods: {
  108. activeIndexSync() {
  109. const path = this.$route.path;
  110. const idx = _.findIndex(this.headerArr, function (o) {
  111. return o.keyPath === path;
  112. });
  113. if (idx > -1) {
  114. this.activeIndex = this.headerArr[idx].keyPath;
  115. } else {
  116. for (const header of this.headerArr) {
  117. if (header.children) {
  118. const idx2 = _.findIndex(header.children, function (o) {
  119. return o.keyPath === path;
  120. });
  121. if (idx2 > -1) {
  122. this.activeIndex = header.keyPath;
  123. return;
  124. }
  125. }
  126. }
  127. }
  128. },
  129. },
  130. mounted() {
  131. console.log('head:', this.$route.query)
  132. },
  133. };
  134. </script>