Ver código fonte

修改错别字

chenrui 3 anos atrás
pai
commit
7865f5d930
1 arquivos alterados com 32 adições e 21 exclusões
  1. 32 21
      src/components/MultiTab/MultiTab.vue

+ 32 - 21
src/components/MultiTab/MultiTab.vue

@@ -6,6 +6,7 @@ export default {
   data () {
     return {
       fullPathList: [],
+      tabsList: [],
       pages: [],
       activeKey: '',
       newTabIndex: 0
@@ -25,7 +26,7 @@ export default {
       }
       this.closeThat(val)
     }).$on('rename', ({ key, name }) => {
-      console.log('rename', key, name)
+      // console.log('rename', key, name)
       try {
         const item = this.pages.find(item => item.path === key)
         item.meta.customTitle = name
@@ -35,7 +36,7 @@ export default {
     })
 
     this.pages.push(this.$route)
-    this.fullPathList.push(this.$route.fullPath)
+    this.tabsList.push(this.$route.name)
     this.selectedLastPath()
   },
   methods: {
@@ -43,30 +44,30 @@ export default {
       this[action](targetKey)
     },
     remove (targetKey) {
-      this.pages = this.pages.filter(page => page.fullPath !== targetKey)
-      this.fullPathList = this.fullPathList.filter(path => path !== targetKey)
+      this.pages = this.pages.filter(page => page.name !== targetKey)
+      this.tabsList = this.tabsList.filter(name => name !== targetKey)
       // 判断当前标签是否关闭,若关闭则跳转到最后一个还存在的标签页
-      if (!this.fullPathList.includes(this.activeKey)) {
+      if (!this.tabsList.includes(this.activeKey)) {
         this.selectedLastPath()
       }
     },
     selectedLastPath () {
-      this.activeKey = this.fullPathList[this.fullPathList.length - 1]
+      this.activeKey = this.tabsList[this.tabsList.length - 1]
     },
 
     // content menu
     closeThat (e) {
       // 判断是否为最后一个标签页,如果是最后一个,则无法被关闭
-      if (this.fullPathList.length > 1) {
+      if (this.tabsList.length > 1) {
         this.remove(e)
       } else {
         this.$message.info('这是最后一个标签了, 无法被关闭')
       }
     },
     closeLeft (e) {
-      const currentIndex = this.fullPathList.indexOf(e)
+      const currentIndex = this.tabsList.indexOf(e)
       if (currentIndex > 0) {
-        this.fullPathList.forEach((item, index) => {
+        this.tabsList.forEach((item, index) => {
           if (index < currentIndex) {
             this.remove(item)
           }
@@ -76,9 +77,9 @@ export default {
       }
     },
     closeRight (e) {
-      const currentIndex = this.fullPathList.indexOf(e)
-      if (currentIndex < (this.fullPathList.length - 1)) {
-        this.fullPathList.forEach((item, index) => {
+      const currentIndex = this.tabsList.indexOf(e)
+      if (currentIndex < (this.tabsList.length - 1)) {
+        this.tabsList.forEach((item, index) => {
           if (index > currentIndex) {
             this.remove(item)
           }
@@ -88,8 +89,8 @@ export default {
       }
     },
     closeAll (e) {
-      const currentIndex = this.fullPathList.indexOf(e)
-      this.fullPathList.forEach((item, index) => {
+      const currentIndex = this.tabsList.indexOf(e)
+      this.tabsList.forEach((item, index) => {
         if (index !== currentIndex) {
           this.remove(item)
         }
@@ -120,15 +121,25 @@ export default {
     }
   },
   watch: {
-    '$route': function (newVal) {
-      this.activeKey = newVal.fullPath
-      if (this.fullPathList.indexOf(newVal.fullPath) < 0) {
-        this.fullPathList.push(newVal.fullPath)
+    '$route': function (newVal, oldVal) {
+      // 父页面(列表)进入子页面(编辑、详情)再返回至父页面时关闭子页面页签
+      if (oldVal && oldVal.name && newVal.query.closeLastOldTab) {
+        this.remove(oldVal.name) // 关闭子页面
+      }
+      this.activeKey = newVal.name
+      const index = this.tabsList.indexOf(newVal.name)
+      // 解决页面地址相同参数不同时打开多个页签问题(例 详情页id或sn不同时)
+      if (index < 0) { // 不存在
+        this.tabsList.push(newVal.name)
         this.pages.push(newVal)
+      } else { // 已存在
+        this.pages.splice(index, 1, newVal)
       }
     },
     activeKey: function (newPathKey) {
-      this.$router.push({ path: newPathKey })
+      const row = this.pages.find(item => item.name == newPathKey)
+      // console.log(row.fullPath)
+      this.$router.push({ path: row.fullPath })
     }
   },
   render () {
@@ -137,10 +148,10 @@ export default {
       return (
         <a-tab-pane
           style={{ height: 0 }}
-          key={page.fullPath} closable={pages.length > 1}
+          key={page.name} closable={pages.length > 1}
         >
           <span slot="tab">
-            {this.renderTabPane(page.meta.customTitle || page.meta.title, page.fullPath)}
+            {this.renderTabPane(page.meta.customTitle || page.meta.title, page.name)}
           </span>
         </a-tab-pane>)
     })