Skip to content

Commit

Permalink
feat(router): reLaunch 移除冗余页面节点 fix #9724
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakaryCode committed Dec 15, 2021
1 parent 9664a9b commit 362fa34
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packages/taro-router/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function processNavigateUrl (option: Option) {
return pathPieces
}

async function navigate (option: Option | NavigateBackOption, method: 'navigateTo' | 'redirectTo' | 'navigateBack') {
async function navigate (option: Option | NavigateBackOption, method: 'navigateTo' | 'navigateBack' | 'redirectTo' | 'reLaunch') {
return new Promise<void>((resolve, reject) => {
const { success, complete, fail } = option
const unListen = history.listen(() => {
Expand All @@ -67,6 +67,9 @@ async function navigate (option: Option | NavigateBackOption, method: 'navigateT
history.push(pathPieces, state)
} else if (method === 'redirectTo') {
history.replace(pathPieces, state)
} else if (method === 'reLaunch') {
stacks.delta = stacks.length
history.replace(pathPieces, state)
}
} else if (method === 'navigateBack') {
stacks.delta = option.delta
Expand Down Expand Up @@ -96,13 +99,11 @@ export function navigateBack (options: NavigateBackOption = { delta: 1 }) {
}

export function switchTab (option: Option) {
// TODO: 清除掉所有的栈去目标页面
return redirectTo(option)
}

export function reLaunch (option: Option) {
// TODO: 清除掉所有的栈去目标页面
return redirectTo(option)
return navigate(option, 'reLaunch')
}

export function getCurrentPages () {
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-router/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ export function createRouter (
return showPage(stacks.getItem(prevIndex), pageConfig, prevIndex)
}
} else {
unloadPage(currentPage)
const delta = stacks.getDelta(pathname)
// NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
unloadPage(currentPage, delta)
}
shouldLoad = true
}
Expand Down

0 comments on commit 362fa34

Please sign in to comment.