index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <zj-page-container>
  3. <el-tabs v-model="activeKey" type="card" @tab-click="tabClick" closable @tab-remove="removeTab">
  4. <el-tab-pane v-for="(item, index) in activeList" :key="item.key" :label="item.label || item.key"
  5. :name="item.key"></el-tab-pane>
  6. </el-tabs>
  7. <zj-page-fill>
  8. <slot :activeKey="showActiveKey" />
  9. </zj-page-fill>
  10. </zj-page-container>
  11. </template>
  12. <script>
  13. export default {
  14. name: "TabPage",
  15. props: {
  16. defaultActives: {
  17. type: Array,
  18. default: () => []
  19. }
  20. },
  21. data() {
  22. return {
  23. activeKey: this?.defaultActives?.length ? this?.defaultActives?.[0]?.key : "",
  24. oldActiveKey: this?.defaultActives?.length ? this?.defaultActives?.[0]?.key : "",
  25. activeList: [...this.defaultActives]
  26. };
  27. },
  28. computed: {
  29. showActiveKey() {
  30. var data = this.activeList.find(item => item.key == this.oldActiveKey)
  31. return data?.activeKey || data?.key || ""
  32. }
  33. },
  34. methods: {
  35. // 点击
  36. tabClick(tab) {
  37. if (this.oldActiveKey != (tab.name || tab)) {
  38. this.activeList.find(item => item.key == this.oldActiveKey)?.closeEvent?.()
  39. this.$nextTick(() => {
  40. this.activeList.find(item => item.key == (tab.name || tab))?.triggerEvent?.()
  41. this.$nextTick(() => {
  42. this.activeKey = (tab.name || tab)
  43. this.oldActiveKey = this.activeKey
  44. })
  45. })
  46. }
  47. },
  48. // 添加
  49. addTab(tabObj) {
  50. if (tabObj.key === undefined) {
  51. console.error("缺少")
  52. return
  53. }
  54. for (var i = 0; i < this.activeList.length; i++) {
  55. if (this.activeList[i].key === tabObj.key) {
  56. this.activeList.splice(i, 1, tabObj);
  57. this.$nextTick(() => {
  58. this.tabClick(tabObj.key)
  59. })
  60. return
  61. }
  62. }
  63. this.activeList.push(tabObj)
  64. this.$nextTick(() => {
  65. this.tabClick(tabObj.key)
  66. })
  67. },
  68. // 删除
  69. removeTab(tab) {
  70. var i;
  71. for (i = 0; i < this.activeList.length; i++) {
  72. if (this.activeList[i].key === tab) {
  73. if (this.activeList[i].essential) {
  74. this.$message.warning(`${this.activeList[i].label || this.activeList[i].key}是必须页面不能删除!`)
  75. return
  76. }
  77. this.activeList[i]?.closeEvent?.()
  78. this.activeList.splice(i, 1);
  79. break
  80. }
  81. }
  82. if (this.activeList[i]) {
  83. this.tabClick(this.activeList[i].key)
  84. return
  85. }
  86. if (this.activeList[i - 1]) {
  87. this.tabClick(this.activeList[i - 1].key)
  88. return
  89. }
  90. this.tabClick("")
  91. },
  92. },
  93. };
  94. </script>
  95. <style lang="scss" scoped>
  96. ::v-deep .el-tabs__header{
  97. margin: 0 0 0 0 !important;
  98. }
  99. </style>