From 1a9966d152d41dbd2ac8284a27171b004c34073f Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 11 Jun 2019 14:29:23 +0800 Subject: [PATCH] fix(h5): navigateBack delta>1 --- src/core/service/api/route.js | 3 ++ src/core/service/plugins/app/router-guard.js | 31 ++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/core/service/api/route.js b/src/core/service/api/route.js index 4f44348a..929b0f12 100644 --- a/src/core/service/api/route.js +++ b/src/core/service/api/route.js @@ -34,6 +34,9 @@ function onAppRoute (type, { } } if (canBack) { + if (delta > 1) { + router._$delta = delta + } router.go(-delta) } break diff --git a/src/core/service/plugins/app/router-guard.js b/src/core/service/plugins/app/router-guard.js index dc281997..4463aa6f 100644 --- a/src/core/service/plugins/app/router-guard.js +++ b/src/core/service/plugins/app/router-guard.js @@ -8,10 +8,18 @@ function addKeepAliveInclude (componentName) { } } -function removeKeepAliveInclude (componentName) { - const index = this.keepAliveInclude.indexOf(componentName) - if (index !== -1) { - this.keepAliveInclude.splice(index, 1) +let deltaIds = [] + +function removeKeepAliveInclude (componentNameOrDelta) { + if (typeof componentNameOrDelta === 'number') { + deltaIds = this.keepAliveInclude.splice(-(componentNameOrDelta - 1)).map(name => { + return parseInt(name.split('-').pop()) + }) + } else { + const index = this.keepAliveInclude.indexOf(componentNameOrDelta) + if (index !== -1) { + this.keepAliveInclude.splice(index, 1) + } } } @@ -117,6 +125,9 @@ function beforeEach (to, from, next, routes) { // 后退或非 API 访问 if (fromId && fromId > toId) { // back removeKeepAliveInclude.call(this, fromName) + if (this.$router._$delta > 1) { + removeKeepAliveInclude.call(this, this.$router._$delta) + } } break } @@ -164,9 +175,19 @@ function afterEach (to, from) { default: if (fromId && fromId > toId) { // history back fromVm && callPageHook(fromVm, 'onUnload') + if (this.$router._$delta > 1) { + deltaIds.reverse().forEach(deltaId => { + const pageVm = currentPages.find(pageVm => pageVm.$page.id === deltaId) + pageVm && callPageHook(pageVm, 'onUnload') + }) + } } break } + + delete this.$router._$delta + deltaIds.length = 0 + if (to.type !== 'reLaunch') { // 因为 reLaunch 会重置 id,故不触发 onShow,switchTab 在 beforeRouteEnter 中触发 // 直接获取所有 pages,getCurrentPages 正常情况下仅返回页面栈内,传 true 则返回所有已存在(主要是 tabBar 页面) const toVm = getCurrentPages(true).find(pageVm => pageVm.$page.id === toId) // 使用最新的 pages @@ -187,4 +208,4 @@ export default function initRouterGuard (appVm, routes) { appVm.$router.afterEach(function (to, from) { afterEach.call(appVm, to, from) }) -} +} -- GitLab