From 3a28b3405d1a68a83e34eda18693036191c536a8 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Tue, 21 Sep 2021 21:36:20 +0800 Subject: [PATCH] u --- index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 8fafdfc..7aa06c1 100644 --- a/index.js +++ b/index.js @@ -33,13 +33,13 @@ module.exports = { }, thisPage(){ - return this.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 - : getCurrentPages()[getCurrentPages().length - 1] + return getCurrentPages()[getCurrentPages().length - 1] // 准备挂在到 window 下使用,因此不必探测 this.$store 了,肯定不存在。 }, localizeText(i18nText) { // 如果直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果通过 this.$T.localeText 访问,那么 this.i18nText 就报错了。因此安全起见,先获取当前 page - const thisPage = thisPage() + const thisPage = 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] || '' } @@ -53,7 +53,8 @@ module.exports = { }, localeText() { - const thisPage = thisPage() // 如果直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果通过 this.$T.localeText 访问,那么 this.i18nText 就报错了。因此安全起见,先获取当前 page + const thisPage = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 + : getCurrentPages()[getCurrentPages().length - 1] return thisPage.i18nText[thisPage.$store.state.i18n.mylang] }, @@ -381,8 +382,9 @@ module.exports = { }) */ showToast({ tool, type, image, title, duration = 2000, ...rest }) { - const pageNow = thisPage() - if (tool === 'uni' || !(pageNow.$refs && pageNow.$refs.toast)) { + const thisPage = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 + : getCurrentPages()[getCurrentPages().length - 1] + if (tool === 'uni' || !(thisPage.$refs && thisPage.$refs.toast)) { // #ifdef APP-PLUS uni.showToast({ icon: 'none', title, duration, ...rest }) // #endif @@ -391,7 +393,7 @@ module.exports = { // #endif } else { // 根据 html 中不同的组件 而不同。 - pageNow.$refs.toast.show({ type, title, duration, ...rest }) + thisPage.$refs.toast.show({ type, title, duration, ...rest }) } },