index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <el-tabs
  3. :value="active"
  4. v-bind="{ ...$attrs }"
  5. v-on="{ ...$listeners, 'tab-click': tab }"
  6. >
  7. <slot />
  8. </el-tabs>
  9. </template>
  10. <script>
  11. import qs from "qs";
  12. import merge from 'webpack-merge'
  13. export default {
  14. name: "tab-router",
  15. props: {
  16. searchKey: {
  17. type: String,
  18. default: "tabrouter",
  19. },
  20. lazy: {
  21. type: Boolean,
  22. defaule: true,
  23. },
  24. value: {
  25. type: String,
  26. default: "",
  27. },
  28. },
  29. data() {
  30. return {
  31. active: "",
  32. panels: [],
  33. panelsByName: {},
  34. };
  35. },
  36. watch: {
  37. active() {
  38. this.$emit('change', this.active)
  39. }
  40. },
  41. methods: {
  42. getAllPanel() {
  43. const data = {};
  44. const routes = [];
  45. this.panels = (this.$slots.default || []).filter((item) => {
  46. const isPanel = item.tag && item.tag.indexOf("ElTabPane") > -1;
  47. if (isPanel && item.child) {
  48. data[item.child.name] = item.child;
  49. }
  50. return isPanel;
  51. });
  52. const search = qs.parse(location.hash.split("?")[1]);
  53. if (this.panels.length) {
  54. this.$nextTick(() => {
  55. const names = this.panels.map(item => item.child?.name)
  56. const sk = search[this.searchKey]
  57. let key = sk && names.includes(sk) ? sk : names[0]
  58. this.active = key;
  59. });
  60. }
  61. this.panelsByName = data;
  62. },
  63. async tab(item, evt) {
  64. // if(this.$attrs['before-leave']){
  65. // try{
  66. // await this.$attrs['before-leave']().then(res=>{
  67. // (res)
  68. // })
  69. // }catch(e){
  70. // (e)
  71. // }
  72. // }
  73. let { query } = this.$route;
  74. const search = qs.stringify({
  75. ...query,
  76. [this.searchKey]: item.name,
  77. });
  78. this.active = item.name;
  79. this.$router.replace({
  80. query: merge(this.$route.query, {
  81. ...query,
  82. [this.searchKey]: item.name,
  83. }),
  84. });
  85. this.$store.dispatch("addVisitedViews", this.$route);
  86. const parentClick = this.$listeners["tab-click"];
  87. if (parentClick) {
  88. parentClick(item, evt);
  89. }
  90. },
  91. },
  92. beforeUpdate() {
  93. this.getAllPanel();
  94. },
  95. mounted() {
  96. this.getAllPanel();
  97. },
  98. };
  99. </script>
  100. <style lang="less" scoped>
  101. </style>