From 61424e89b6677b31976ed5d1472f8f61ec92a7ad Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Mon, 15 May 2023 23:17:59 +0800 Subject: [PATCH] update this.thisPage() to this.thisPage?.() --- unitool.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/unitool.js b/unitool.js index 8dc7f36..95bee0f 100644 --- a/unitool.js +++ b/unitool.js @@ -37,10 +37,9 @@ export default { localizeText (i18nText) { i18nText = i18nText || // 如果传入i18n参数 ({zhCN:'...', enUS:'...'}) - (this?.__page__ - ? this.i18nText // 1) 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。2) 对于组件内定义的 i18nText,要使用 this 来获得组件内的 i18nText - : this.thisPage()?.i18nText) // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就报错了。因此通过 thisPage().i18nText 访问。 - const mylang = wo?.ss?.i18n?.mylang // this.thisPage() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里,getApp().$store.state 未定义 + this.i18nText || // 1) 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。2) 对于组件内定义的 i18nText,要使用 this 来获得组件内的 i18nText + this.thisPage?.()?.i18nText // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就报错了。因此通过 thisPage?.().i18nText 访问。 + const mylang = wo?.ss?.i18n?.mylang // this.thisPage?.() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里,getApp().$store.state 未定义 return i18nText?.[mylang] || (typeof i18nText === 'string' ? i18nText : '') // 必须检测是否string,如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错 }, @@ -51,7 +50,7 @@ export default { setBarTitles ({ windowTitle, pageTitle, pagesJson = this.pagesJson || wo?.pagesJson, envar = this.envar || wo?.envar } = {}) { const mylang = getApp()?.$store?.state?.i18n?.mylang // 不要用 pageNow.$store,防止在 App.vue 里无法获取当前页面。 - const pageNow = this.thisPage() // 需要兼顾在 App.vue 时无法获取当前页面的情况,因为如果在 topWindow 里调用本函数,getApp() 和 getCurrentPages()[getCurrentPages().length-1] 就是 undefined。 + const pageNow = this.thisPage?.() // 需要兼顾在 App.vue 时无法获取当前页面的情况,因为如果在 topWindow 里调用本函数,getApp() 和 getCurrentPages()[getCurrentPages().length-1] 就是 undefined。 const navibarTitle = pageTitle || @@ -145,7 +144,7 @@ export default { apiWhat = {}, timeout, }) { - const thisRoute = this.thisPage()?.route || 'VueApp' // 立刻保存 this.thisPage().route,因为在调用后台后,可能已切换到了其他页面。 + const thisRoute = this.thisPage?.()?.route || 'VueApp' // 立刻保存 this.thisPage?.().route,因为在调用后台后,可能已切换到了其他页面。 const startTime = new Date().toJSON() let url = undefined let result = {} @@ -426,19 +425,19 @@ export default { callback // 发生在 toast 之后 }) */ - showToast ({ tool, type, image, title, duration = 2000, ...rest }) { + showToast ({ tool, type = 'success', image, title, duration = 2000, ...rest }) { // rename to popToast? if (tool !== 'uni') { // 来自 - const toast = this.thisPage()?.$refs?.toast || getApp().globalData.toast || wo.toast - const popup = this.thisPage()?.$refs?.popup || getApp().globalData.popup || wo.popup + const toast = this.thisPage?.()?.$refs?.toast || getApp().globalData.toast || wo.toast + const popup = this.thisPage?.()?.$refs?.popup || getApp().globalData.popup || wo.popup if (toast) { toast.show({ type, title, duration, ...rest }) return } else if (popup) { wo.ss.popMessage = title - wo.ss.popType = type || 'success' // success/error/warning/info - wo.ss.popDuration = duration || 2000 + wo.ss.popType = type // success/error/warning/info + wo.ss.popDuration = duration popup.open() return } @@ -566,7 +565,7 @@ export default { } }, - next_focus (currentFocus, focusList = this.thisPage().focusList) { + next_focus (currentFocus, focusList = this.thisPage?.()?.focusList) { if (focusList) { for (let n in focusList) { focusList[n] = false