From 13ec437ef86204cc7dfe982da00e3520eb368d34 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 28 Dec 2018 17:04:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A1=B5=E9=9D=A2=20o?= =?UTF-8?q?nUnload=20=E4=B8=8D=E8=A7=A6=E5=8F=91=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/service/plugins/app/router-guard.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/core/service/plugins/app/router-guard.js b/src/core/service/plugins/app/router-guard.js index a91134eef..d38aab24e 100644 --- a/src/core/service/plugins/app/router-guard.js +++ b/src/core/service/plugins/app/router-guard.js @@ -38,7 +38,10 @@ function reLaunch (toName) { this.keepAliveInclude = [] } +let currentPages = [] + function beforeEach (to, from, next, routes) { + currentPages = getCurrentPages(true) // 每次 beforeEach 时获取当前currentPages,因为 afterEach 之后,获取不到上一个 page 了,导致无法调用 onUnload const fromId = from.params.__id__ const toId = to.params.__id__ if (toId === fromId) { // 相同页面阻止 @@ -97,7 +100,7 @@ function afterEach (to, from) { const fromId = from.params.__id__ const toId = to.params.__id__ - const fromVm = getCurrentPages(true).find(pageVm => pageVm.$page.id === fromId) + const fromVm = currentPages.find(pageVm => pageVm.$page.id === fromId) // 使用 beforeEach 时的 pages switch (to.type) { case 'navigateTo': // 前一个页面触发 onHide @@ -121,10 +124,10 @@ function afterEach (to, from) { } if (to.type !== 'reLaunch') { // 因为 reLaunch 会重置 id,故不触发 onShow,switchTab 在 beforeRouteEnter 中触发 // 直接获取所有 pages,getCurrentPages 正常情况下仅返回页面栈内,传 true 则返回所有已存在(主要是 tabBar 页面) - const toVm = getCurrentPages(true).find(pageVm => pageVm.$page.id === toId) + const toVm = getCurrentPages(true).find(pageVm => pageVm.$page.id === toId) // 使用最新的 pages if (toVm) { // 目标页面若已存在,则触发 onShow // 延迟执行 onShow,防止与 UniServiceJSBridge.emit('onHidePopup') 冲突。 - setTimeout(function () { + setTimeout(function () { callPageHook(toVm, 'onShow') }, 0) } -- GitLab