From 8b501429d356507cddc2a272e61f642c403488c4 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Fri, 1 Apr 2022 20:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=20this.constructor.name=3D=3D=3D'VueC?= =?UTF-8?q?omponent'=20=E6=9D=A5=E6=A3=80=E6=B5=8B=E6=98=AF=E5=90=A6vue?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 1a179f5..6cafecb 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,9 @@ export default { BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER 或云服务 UNICLOUD. 应当根据实际需要,在前端所用的 unitool 里覆盖。 thisPage() { - return getCurrentPages()[getCurrentPages().length - 1] || {} // 准备挂在到 window 下使用,因此不必探测 this?.$store 了,肯定不存在。用 {} 做备份是为了在 App.vue 中使用时,getCurrentPages() 有可能获取不到。 + return this.constructor.name==='VueComponent' ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 + : ( getCurrentPages()[getCurrentPages().length - 1] // [20220401] 发现在 topWindow 里, getCurrentPages 是 undefined。 + || {} ) // 用 {} 做备份是为了在 App.vue 中使用时,getCurrentPages() 有可能获取不到。 }, // 输出命令行提示,可用来取代 console.log/info/warn/error @@ -55,10 +57,8 @@ export default { }, localizeText(i18nText) { - const mylang = getApp().$store.state.i18n.mylang // pageNow 有可能不存在,所以用 getApp().$store 更安全 - const pageNow = this.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 - : getCurrentPages()[getCurrentPages().length - 1] // [20220401] 发现在 topWindow 里, 获取的 pageNow 是空的。因此在 topWindow 里不要使用 i18nText 字段。 - i18nText = i18nText || pageNow?.i18nText // 如果本方法直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果挂载到 wo 下,那么 this.i18nText 就报错了。因此安全起见,通过 pageNow.i18nText 访问。 + const mylang = getApp().$store.state.i18n.mylang // thisPage() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全 + i18nText = i18nText || thisPage()?.i18nText // 如果本方法直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果挂载到 wo 下,那么 this.i18nText 就报错了。因此安全起见,通过 pageNow.i18nText 访问。 if (i18nText && typeof (i18nText) === 'object' && mylang) { return i18nText[mylang] || '' } @@ -69,9 +69,7 @@ export default { }, localeText() { - const pageNow = this?.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组件内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 - : getCurrentPages()[getCurrentPages().length - 1] - return pageNow?.i18nText?.[getApp().$store.state.i18n.mylang] || {} + return thisPage().pageNow?.i18nText?.[getApp().$store.state.i18n.mylang] || {} }, setBarTitles ({ windowTitle, pageTitle } = {}) { @@ -437,8 +435,7 @@ export default { }) */ showToast({ tool, type, image, title, duration = 2000, ...rest }) { - const pageNow = this?.$store ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 - : getCurrentPages()[getCurrentPages().length - 1] + const pageNow = thisPage() if (tool === 'uni' || ! pageNow?.$refs?.toast) { // #ifdef APP-PLUS uni.showToast({ icon: 'none', title, duration, ...rest })