index.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <div>
  3. <el-tabs :value="active" @tab-click="tab">
  4. <slot/>
  5. </el-tabs>
  6. <router-view></router-view>
  7. </div>
  8. </template>
  9. <script>
  10. import qs from 'qs'
  11. export default {
  12. name: 'tab-router',
  13. props: {
  14. searchKey: {
  15. type: String,
  16. default: 'tabrouter'
  17. }
  18. },
  19. data() {
  20. return {
  21. active: '',
  22. panels: [],
  23. panelsByName: {}
  24. }
  25. },
  26. methods: {
  27. getAllPanel() {
  28. const data = {}
  29. const routes = []
  30. this.panels = this.$slots.default.filter(item => {
  31. const isPanel = item.tag && item.tag.indexOf('ElTabPane') > -1
  32. if (isPanel && item.child) {
  33. data[item.child.name] = item.child
  34. }
  35. return isPanel
  36. })
  37. this.panelsByName = data
  38. },
  39. tab(item) {
  40. const { query } = this.$route
  41. const search = qs.stringify({
  42. ...query,
  43. [this.searchKey]: item.name
  44. })
  45. history.replaceState(location.pathname, null, `#${this.$route.path}?${search}`)
  46. }
  47. },
  48. beforeUpdate() {
  49. this.getAllPanel()
  50. },
  51. mounted() {
  52. this.getAllPanel()
  53. if (this.panels.length) {
  54. this.active = this.$route.query[this.searchKey] || this.panels[0].child?.name
  55. }
  56. }
  57. }
  58. </script>
  59. <style lang="less" scoped>
  60. </style>