From e8f55847e0e48c7a5984e1a52a8bd6c86bb34073 Mon Sep 17 00:00:00 2001 From: qiang Date: Wed, 6 Jan 2021 10:56:53 +0800 Subject: [PATCH] fix: unload --- src/core/service/plugins/app/router-guard.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/core/service/plugins/app/router-guard.js b/src/core/service/plugins/app/router-guard.js index b3e4781949..98bca043c9 100644 --- a/src/core/service/plugins/app/router-guard.js +++ b/src/core/service/plugins/app/router-guard.js @@ -167,12 +167,22 @@ function afterEach (to, from) { const fromVm = currentPages.find(pageVm => pageVm.$page.id === fromId) // 使用 beforeEach 时的 pages + function unloadPage (vm) { + if (vm) { + callPageHook(vm, 'onUnload') + const index = currentPages.indexOf(vm) + if (index >= 0) { + currentPages.splice(index, 1) + } + } + } + switch (to.type) { case 'navigateTo': // 前一个页面触发 onHide fromVm && callPageHook(fromVm, 'onHide') break case 'redirectTo': // 前一个页面触发 onUnload - fromVm && callPageHook(fromVm, 'onUnload') + unloadPage(fromVm) break case 'switchTab': if (from.meta.isTabBar) { // 前一个页面是 tabBar 触发 onHide,非 tabBar 页面在 beforeEach 中已触发 onUnload @@ -183,11 +193,11 @@ function afterEach (to, from) { break default: if (fromId && fromId > toId) { // history back - fromVm && callPageHook(fromVm, 'onUnload') + unloadPage(fromVm) if (this.$router._$delta > 1) { deltaIds.reverse().forEach(deltaId => { const pageVm = currentPages.find(pageVm => pageVm.$page.id === deltaId) - pageVm && callPageHook(pageVm, 'onUnload') + unloadPage(pageVm) }) } } -- GitLab