From 40fd9c4c2c02af934bd769a3a1410d34231869ad Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Tue, 5 Apr 2022 22:43:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E8=83=BD=E5=9C=A8=20production=20?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=8B=E4=BD=BF=E7=94=A8=20constructor.nam?= =?UTF-8?q?e=3D=3D=3D'VueComponent'=EF=BC=8C=E6=8D=A2=E7=94=A8=20=5F=5Fpag?= =?UTF-8?q?e=5F=5F=20=E6=9D=A5=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usertool.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/usertool.js b/usertool.js index 2abf468..1381fae 100644 --- a/usertool.js +++ b/usertool.js @@ -16,7 +16,7 @@ export default { BACKEND_DEFAULT: 'SERVER', // 服务器 SERVER 或云服务 UNICLOUD thisPage() { - return this.constructor.name==='VueComponent' ? this // 对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 + return this.__page__ ? this // 1) constructor.name==='VueComponent' 只在 development 环境有用,在 production 环境会被简化成 'o'。2)对于组件内定义的 i18nText,要使用 this 来获得组建内的 i18nText,而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 : ( getCurrentPages()[getCurrentPages().length - 1] // [20220401] 发现在 topWindow 里, getCurrentPages 是 undefined。 || {} ) // 用 {} 做备份是为了在 App.vue 中使用时,getCurrentPages() 有可能获取不到。 }, @@ -46,7 +46,7 @@ export default { }, localizeText(i18nText) { - i18nText = i18nText?.constructor?.name==='VueComponent' ? this.i18nText // 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。 + i18nText = i18nText?.__page__ ? this.i18nText // 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 i18nText 会被自动赋值为 this 就是当前页面,直接取用 this.i18nText 即可。 : ( i18nText // 如果传入i18n参数 ({zhCN:'...', enUS:'...'}) || this.thisPage()?.i18nText) // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就报错了。因此通过 thisPage().i18nText 访问。 const mylang = getApp().$store.state.i18n.mylang // this.thisPage() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全