提交 1a9966d1 编写于 作者: fxy060608's avatar fxy060608

fix(h5): navigateBack delta>1

上级 a0f5d782
......@@ -34,6 +34,9 @@ function onAppRoute (type, {
}
}
if (canBack) {
if (delta > 1) {
router._$delta = delta
}
router.go(-delta)
}
break
......
......@@ -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)
})
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册