From 484d3b09174b7458e11be2a82ee0a567eec5f89f Mon Sep 17 00:00:00 2001 From: Blet <412986558@qq.com> Date: Tue, 24 Dec 2019 12:40:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(router):=20H5=E4=B8=8B=20ReLaunch=20?= =?UTF-8?q?=E6=B2=A1=E5=8A=9E=E6=B3=95=E8=B7=B3=E5=88=B0=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E7=9A=84=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-router/.prettierrc | 7 ++ packages/taro-router/src/apis/index.ts | 96 +++++++++++++++++--------- 2 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 packages/taro-router/.prettierrc diff --git a/packages/taro-router/.prettierrc b/packages/taro-router/.prettierrc new file mode 100644 index 000000000000..f679e842d58e --- /dev/null +++ b/packages/taro-router/.prettierrc @@ -0,0 +1,7 @@ +{ + "tabWidth": 2, + "singleQuote": true, + "jsxSingleQuote": true, + "semi": false, + "printWidth": 80 +} diff --git a/packages/taro-router/src/apis/index.ts b/packages/taro-router/src/apis/index.ts index 1ab498cecf94..fdff6d50753d 100644 --- a/packages/taro-router/src/apis/index.ts +++ b/packages/taro-router/src/apis/index.ts @@ -44,9 +44,7 @@ let currentPagename = '' const relaunchUrlKey = '__relaunchUrl' const addHtmlExtname = (str: string) => { - return /\.html\b/.test(str) - ? str - : `${str}.html` + return /\.html\b/.test(str) ? str : `${str}.html` } const getTargetUrl = (url: string, customRoutes: CustomRoutes) => { @@ -55,12 +53,19 @@ const getTargetUrl = (url: string, customRoutes: CustomRoutes) => { const search = matched[2] || '' const targetUrl = rp(pathname, currentPagename) - const nextPagename = addHtmlExtname(stripLeadingSlash(customRoutes[targetUrl] || targetUrl)) + const nextPagename = addHtmlExtname( + stripLeadingSlash(customRoutes[targetUrl] || targetUrl) + ) return `${basename}/${nextPagename}${search}` } -const createNavigateTo = ({ customRoutes }: RouterConfig, history?: History) => { - return function ({ url }: NavigateToOption): Promise { +const createNavigateTo = ( + { customRoutes }: RouterConfig, + history?: History +) => { + return function({ + url + }: NavigateToOption): Promise { const res: Taro.General.CallbackResult = { errMsg: '' } @@ -83,14 +88,22 @@ const createNavigateTo = ({ customRoutes }: RouterConfig, history?: History) => } } -const createNavigateBack = ({ customRoutes }: RouterConfig, history?: History) => { - return function (opts: NavigateBackOption = {}): Promise { +const createNavigateBack = ( + { customRoutes }: RouterConfig, + history?: History +) => { + return function( + opts: NavigateBackOption = {} + ): Promise { const res: Taro.General.CallbackResult = { errMsg: '' } try { const { delta = 1 } = opts - invariant(delta >= 0, 'navigateBack must be called with a delta greater than 0') + invariant( + delta >= 0, + 'navigateBack must be called with a delta greater than 0' + ) if (history) { history.go(-delta) } else { @@ -106,8 +119,13 @@ const createNavigateBack = ({ customRoutes }: RouterConfig, history?: History) = } } -const createRedirectTo = ({ customRoutes }: RouterConfig, history?: History) => { - return function ({ url }: RedirectToOption): Promise { +const createRedirectTo = ( + { customRoutes }: RouterConfig, + history?: History +) => { + return function({ + url + }: RedirectToOption): Promise { const res: Taro.General.CallbackResult = { errMsg: '' } @@ -116,8 +134,9 @@ const createRedirectTo = ({ customRoutes }: RouterConfig, history?: History) => invariant(url, 'redirectTo must be called with a url') if (/^(https?:)\/\//.test(url)) { - window.location.assign(url); - } if (history) { + window.location.assign(url) + } + if (history) { history.replace(url) } else { window.location.replace(getTargetUrl(url, customRoutes)) @@ -141,28 +160,41 @@ const createReLaunch = ({ customRoutes }: RouterConfig, history?: History) => { } catch (e) { console.log(e.message) } - return function ({ url }): Promise { - const res: Taro.General.CallbackResult = { - errMsg: '' - } - try { - if (history) { - history.go(-(history.length - 1)) - if (/^(https?:)\/\//.test(url)) { - window.location.assign(url); + return function({ url }): Promise { + return new Promise((resolve, reject) => { + const res: Taro.General.CallbackResult = { + errMsg: '' + } + try { + // setTimeout hack + // 修复 history.go 之后,后面的代码不执行的问题 + setTimeout(() => { + if (history) { + if (/^(https?:)\/\//.test(url)) { + window.location.assign(url) + } else { + history.replace(url) + } + } else { + localStorage.setItem( + relaunchUrlKey, + getTargetUrl(url, customRoutes) + ) + window.history.go(-(window.history.length - 1)) + } + res.errMsg = 'reLaunch:ok' + resolve(res) + }, 50) + if (history) { + history.go(-(history.length - 1)) } else { - history.replace(url) + window.history.go(-(window.history.length - 1)) } - } else { - localStorage.setItem(relaunchUrlKey, getTargetUrl(url, customRoutes)) - window.history.go(-(window.history.length - 1)) + } catch (e) { + res.errMsg = `reLaunch:fail ${e.message}` + reject(res) } - res.errMsg = 'reLaunch:ok' - return Promise.resolve(res) - } catch (e) { - res.errMsg = `reLaunch:fail ${e.message}` - return Promise.reject(res) - } + }) } }