diff --git a/packages/taro-router/src/api.ts b/packages/taro-router/src/api.ts index 9e9639d7331e..3b9f45f6127a 100644 --- a/packages/taro-router/src/api.ts +++ b/packages/taro-router/src/api.ts @@ -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((resolve, reject) => { const { success, complete, fail } = option const unListen = history.listen(() => { @@ -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 @@ -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 () { diff --git a/packages/taro-router/src/router/index.ts b/packages/taro-router/src/router/index.ts index 5d8392ecc56d..df01fdfb4b7d 100644 --- a/packages/taro-router/src/router/index.ts +++ b/packages/taro-router/src/router/index.ts @@ -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 }