header.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. export default {
  34. name: 'mtaHeader',
  35. props: {
  36. activeNav: { // 默认选中的 路径 与组件中的index相互对应
  37. type: String,
  38. require: true,
  39. default: '0',
  40. },
  41. },
  42. components: {},
  43. data() {
  44. return {
  45. activeIndex: this.activeNav,
  46. headerArr: [
  47. {
  48. content: '首页',
  49. keyPath: '/pages/index',
  50. },
  51. {
  52. content: '考试平台',
  53. keyPath: '/examsystem',
  54. children: [],
  55. },
  56. {
  57. content: '培训平台',
  58. keyPath: '/pages/trainsystem',
  59. children: [],
  60. },
  61. {
  62. content: '课程开发',
  63. keyPath: '/pages/courseDev',
  64. children: [],
  65. },
  66. {
  67. content: '新闻资讯',
  68. keyPath: '/pages/newsInfor',
  69. children: [],
  70. },
  71. {
  72. content: '帮助中心',
  73. keyPath: '/pages/helpCenter',
  74. children: [],
  75. }, {
  76. content: '关于我们',
  77. keyPath: '/pages/aboutUs',
  78. children: [],
  79. },
  80. ],
  81. };
  82. },
  83. watch: {
  84. // 监听
  85. '$route.path': {
  86. handler(newVal, oldVal) {
  87. this.activeIndexSync();
  88. },
  89. immediate: true,
  90. },
  91. /* $route: {
  92. handler: function(val, oldVal){
  93. debugger;
  94. this.activeIndexSync();
  95. },
  96. // 深度观察监听
  97. deep: true
  98. }*/
  99. },
  100. computed: {
  101. },
  102. methods: {
  103. activeIndexSync() {
  104. const path = this.$route.path;
  105. console.log(path);
  106. return
  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>