From 6d2d24920a934e35059f038f87b0c83e88df925c Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 17 Apr 2019 15:51:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(h5):=20tabBar=20=E5=88=87=E6=8D=A2=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E6=BB=9A=E5=8A=A8=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/service/plugins/app/router-guard.js | 35 ++++++++++++++++---- src/core/service/plugins/index.js | 23 +++++++++++-- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/core/service/plugins/app/router-guard.js b/src/core/service/plugins/app/router-guard.js index a0e36bee15..9eb4223600 100644 --- a/src/core/service/plugins/app/router-guard.js +++ b/src/core/service/plugins/app/router-guard.js @@ -15,7 +15,28 @@ function removeKeepAliveInclude (componentName) { } } -function switchTab (routes) { +let positionStore = Object.create(null) + +export function getTabBarScrollPosition (id) { + return positionStore[id] +} + +function saveTabBarScrollPosition (id) { + positionStore[id] = { + x: window.pageXOffset, + y: window.pageYOffset + } +} + +function switchTab (routes, to, from) { + if ( + to && + from && + to.meta.isTabBar && + from.meta.isTabBar + ) { // tabbar 跳 tabbar + saveTabBarScrollPosition(from.params.__id__) + } // 关闭非 tabBar 页面 const pages = getCurrentPages() for (let i = pages.length - 1; i >= 0; i--) { @@ -33,11 +54,13 @@ function reLaunch (toName) { // 关闭所有页面 const pages = getCurrentPages(true) for (let i = pages.length - 1; i >= 0; i--) { - callPageHook(pages[i], 'onUnload') - // 重新reLaunch至首页可能会被keepAlive,先手动强制destroy + callPageHook(pages[i], 'onUnload') + // 重新reLaunch至首页可能会被keepAlive,先手动强制destroy pages[i].$destroy() } - this.keepAliveInclude = [] + this.keepAliveInclude = [] + // 清空 positionStore + positionStore = Object.create(null) } let currentPages = [] @@ -65,7 +88,7 @@ function beforeEach (to, from, next, routes) { removeKeepAliveInclude.call(this, fromName) if (from.meta) { if (from.meta.isQuit) { // 如果 redirectTo 的前一个页面是 quit 类型,则新打开的页面也是 quit - to.meta.isQuit = true + to.meta.isQuit = true to.meta.isEntry = !!from.meta.isEntry } if (from.meta.isTabBar) { // 如果是 tabBar,需要更新系统组件 tabBar 内的 list 数据 @@ -78,7 +101,7 @@ function beforeEach (to, from, next, routes) { break case 'switchTab': - switchTab.call(this, routes) + switchTab.call(this, routes, to, from) break case 'reLaunch': reLaunch.call(this, toName) diff --git a/src/core/service/plugins/index.js b/src/core/service/plugins/index.js index 1f2c8262ad..b961a5587f 100644 --- a/src/core/service/plugins/index.js +++ b/src/core/service/plugins/index.js @@ -16,6 +16,10 @@ import { createPageMixin } from './page' +import { + getTabBarScrollPosition +} from './app/router-guard' + function getMinId (routes) { let minId = 0 routes.forEach(route => { @@ -62,6 +66,17 @@ export default { if (savedPosition) { return savedPosition } else { + if ( + to && + from && + to.meta.isTabBar && + from.meta.isTabBar + ) { // tabbar 跳 tabbar + const position = getTabBarScrollPosition(to.params.__id__) + if (position) { + return position + } + } return { x: 0, y: 0 @@ -101,7 +116,9 @@ export default { const appMixin = createAppMixin(routes, entryRoute) // mixin app hooks Object.keys(appMixin).forEach(hook => { - options[hook] = options[hook] ? [].concat(appMixin[hook], options[hook]) : [appMixin[hook]] + options[hook] = options[hook] ? [].concat(appMixin[hook], options[hook]) : [ + appMixin[hook] + ] }) // router @@ -117,7 +134,9 @@ export default { const pageMixin = createPageMixin() // mixin page hooks Object.keys(pageMixin).forEach(hook => { - options[hook] = options[hook] ? [].concat(pageMixin[hook], options[hook]) : [pageMixin[hook]] + options[hook] = options[hook] ? [].concat(pageMixin[hook], options[hook]) : [ + pageMixin[hook] + ] }) } else { if (this.$parent && this.$parent.__page__) { -- GitLab