From 05bbcd8289752176741e5644e8a3413dffb60e48 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Wed, 6 Oct 2021 12:20:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E7=8E=B0=20localizeText=20=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E5=88=B0=20globalThis=20=E6=97=B6=EF=BC=8C=E5=9C=A8=E9=87=8C=E8=B0=83=E7=94=A8=E5=87=BA=E9=94=99=20this.$s?= =?UTF-8?q?tore=20=E5=9B=A0=E4=B8=BA=20this=20=E7=AB=9F=E7=84=B6=20undefin?= =?UTF-8?q?ed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index f02fe86..1e979da 100644 --- a/index.js +++ b/index.js @@ -32,18 +32,20 @@ module.exports = { }, thisPage() { - return getCurrentPages()[getCurrentPages().length - 1] // 准备挂在到 window 下使用,因此不必探测 this.$store 了,肯定不存在。 + return getCurrentPages()[getCurrentPages().length - 1] // 准备挂在到 window 下使用,因此不必探测 this?.$store 了,肯定不存在。 }, localizeText(i18nText) { // 如果直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果通过 this.$T.localeText 访问,那么 this.i18nText 就报错了。因此安全起见,先获取当前 page - const thisPage = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 + const pageNow = this?.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 : getCurrentPages()[getCurrentPages().length - 1] - if (i18nText && typeof(i18nText)==='object' && thisPage.$store?.state?.i18n?.mylang) { - return i18nText[thisPage.$store.state.i18n.mylang] || '' + // 支持 localizeText({...}) 来解析参数 + if (i18nText && typeof(i18nText)==='object' && pageNow.$store?.state?.i18n?.mylang) { + return i18nText[pageNow.$store.state.i18n.mylang] || '' } - if (!i18nText && typeof(thisPage.i18nText)==='object' && thisPage.$store?.state?.i18n?.mylang) { - return thisPage.i18nText[thisPage.$store.state.i18n.mylang] || '' + // 支持 localizeText() 来解析 页面.vue 的顶层 i18nText + if (!i18nText && typeof(pageNow.i18nText)==='object' && pageNow.$store?.state?.i18n?.mylang) { + return pageNow.i18nText[pageNow.$store.state.i18n.mylang] || '' } if (typeof i18nText === 'string'){ return i18nText @@ -52,9 +54,9 @@ module.exports = { }, localeText() { - const thisPage = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组件内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 + const pageNow = this?.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组件内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 : getCurrentPages()[getCurrentPages().length - 1] - return thisPage.i18nText?.[thisPage.$store.state.i18n.mylang] || {} + return pageNow.i18nText?.[pageNow.$store.state.i18n.mylang] || {} }, makeServerUrl(route = '') { @@ -372,9 +374,9 @@ module.exports = { }) */ showToast({ tool, type, image, title, duration = 2000, ...rest }) { - const thisPage = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 + const pageNow = this?.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 : getCurrentPages()[getCurrentPages().length - 1] - if (tool === 'uni' || !(thisPage.$refs && thisPage.$refs.toast)) { + if (tool === 'uni' || !(pageNow.$refs && pageNow.$refs.toast)) { // #ifdef APP-PLUS uni.showToast({ icon: 'none', title, duration, ...rest }) // #endif @@ -383,7 +385,7 @@ module.exports = { // #endif } else { // 根据 html 中不同的组件 而不同。 - thisPage.$refs.toast.show({ type, title, duration, ...rest }) + pageNow.$refs.toast.show({ type, title, duration, ...rest }) } },