|
|
@@ -37,18 +37,28 @@ export default {
|
|
|
this.pages.push(this.$route)
|
|
|
this.fullPathList.push(this.$route.fullPath)
|
|
|
this.selectedLastPath()
|
|
|
+ // 初始化时将当前页面组件加入缓存
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.addCurrentToCache()
|
|
|
+ })
|
|
|
},
|
|
|
methods: {
|
|
|
onEdit (targetKey, action) {
|
|
|
this[action](targetKey)
|
|
|
},
|
|
|
remove (targetKey) {
|
|
|
+ const closedPage = this.pages.find(page => page.fullPath === targetKey)
|
|
|
this.pages = this.pages.filter(page => page.fullPath !== targetKey)
|
|
|
this.fullPathList = this.fullPathList.filter(path => path !== targetKey)
|
|
|
// 判断当前标签是否关闭,若关闭则跳转到最后一个还存在的标签页
|
|
|
- if (!this.fullPathList.includes(this.activeKey)) {
|
|
|
+ const needNavigate = !this.fullPathList.includes(this.activeKey)
|
|
|
+ if (needNavigate) {
|
|
|
this.selectedLastPath()
|
|
|
}
|
|
|
+ // 清理已关闭标签页的组件缓存
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.removePageFromCache(closedPage)
|
|
|
+ })
|
|
|
},
|
|
|
selectedLastPath () {
|
|
|
this.activeKey = this.fullPathList[this.fullPathList.length - 1]
|
|
|
@@ -117,6 +127,49 @@ export default {
|
|
|
<span style={{ userSelect: 'none' }}>{ title }</span>
|
|
|
</a-dropdown>
|
|
|
)
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 解析路由对应的组件名称
|
|
|
+ * 优先从 $route.matched 获取已解析的组件 name,回退到路由 name
|
|
|
+ */
|
|
|
+ getRouteComponentName (route) {
|
|
|
+ if (!route) return null
|
|
|
+ const layoutComponents = ['BasicLayout', 'RouteView', 'PageView', 'UserLayout', 'BlankLayout']
|
|
|
+ // 从 matched 路由记录中获取组件名称
|
|
|
+ if (route.matched && route.matched.length > 0) {
|
|
|
+ const lastRecord = route.matched[route.matched.length - 1]
|
|
|
+ const component = lastRecord.components && lastRecord.components.default
|
|
|
+ if (component && component.name && !layoutComponents.includes(component.name)) {
|
|
|
+ return component.name
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 回退到路由名称
|
|
|
+ return route.name || null
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 将当前路由组件加入缓存
|
|
|
+ */
|
|
|
+ addCurrentToCache () {
|
|
|
+ const name = this.getRouteComponentName(this.$route)
|
|
|
+ if (name) {
|
|
|
+ this.$store.dispatch('AddCachedView', name)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 关闭标签页时,从缓存中移除对应组件
|
|
|
+ * 仅当没有其他打开的标签使用同一组件时才移除
|
|
|
+ */
|
|
|
+ removePageFromCache (closedPage) {
|
|
|
+ if (!closedPage) return
|
|
|
+ const componentName = this.getRouteComponentName(closedPage)
|
|
|
+ if (!componentName) return
|
|
|
+ // 检查是否还有其他打开的标签页使用同一组件
|
|
|
+ const stillInUse = this.pages.some(page => {
|
|
|
+ return this.getRouteComponentName(page) === componentName
|
|
|
+ })
|
|
|
+ if (!stillInUse) {
|
|
|
+ this.$store.dispatch('DelCachedView', componentName)
|
|
|
+ }
|
|
|
}
|
|
|
},
|
|
|
watch: {
|
|
|
@@ -126,6 +179,10 @@ export default {
|
|
|
this.fullPathList.push(newVal.fullPath)
|
|
|
this.pages.push(newVal)
|
|
|
}
|
|
|
+ // 将当前页面组件加入缓存保活列表
|
|
|
+ this.$nextTick(() => {
|
|
|
+ this.addCurrentToCache()
|
|
|
+ })
|
|
|
},
|
|
|
activeKey: function (newPathKey) {
|
|
|
this.$router.push({ path: newPathKey })
|