发现 localizeText 绑定到 globalThis 时,在<template>里调用出错 this.$store 因为 this 竟然 undefined

This commit is contained in:
陆柯 2021-10-06 12:20:51 +08:00
parent dc8dd18f3a
commit 05bbcd8289

View File

@ -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 中不同的组件 <ucToast/> 或 <toast/> 而不同。
thisPage.$refs.toast.show({ type, title, duration, ...rest })
pageNow.$refs.toast.show({ type, title, duration, ...rest })
}
},