header.vue 3.4 KB

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