用 this.constructor.name==='VueComponent' 来检测是否vue页面

This commit is contained in:
陆柯 2022-04-01 20:52:31 +08:00
parent cbea8a6d0c
commit 8b501429d3

View File

@ -19,7 +19,9 @@ export default {
BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER 或云服务 UNICLOUD. 应当根据实际需要,在前端所用的 unitool 里覆盖。 BACKEND: 'SERVER', // 通过变量来动态切换后台类型:服务器 SERVER 或云服务 UNICLOUD. 应当根据实际需要,在前端所用的 unitool 里覆盖。
thisPage() { 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 // 输出命令行提示,可用来取代 console.log/info/warn/error
@ -55,10 +57,8 @@ export default {
}, },
localizeText(i18nText) { localizeText(i18nText) {
const mylang = getApp().$store.state.i18n.mylang // pageNow 有可能不存在,所以用 getApp().$store 更安全 const mylang = getApp().$store.state.i18n.mylang // thisPage() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全
const pageNow = this.$store ? this // 对于组件内定义的 i18nText要使用 this 来获得组建内的 i18nText而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 i18nText = i18nText || thisPage()?.i18nText // 如果本方法直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果挂载到 wo 下,那么 this.i18nText 就报错了。因此安全起见,通过 pageNow.i18nText 访问。
: getCurrentPages()[getCurrentPages().length - 1] // [20220401] 发现在 topWindow 里, 获取的 pageNow 是空的。因此在 topWindow 里不要使用 i18nText 字段。
i18nText = i18nText || pageNow?.i18nText // 如果本方法直接挂载到 Vue.prototype 下,那么可以直接访问 this.i18nText。但如果挂载到 wo 下,那么 this.i18nText 就报错了。因此安全起见,通过 pageNow.i18nText 访问。
if (i18nText && typeof (i18nText) === 'object' && mylang) { if (i18nText && typeof (i18nText) === 'object' && mylang) {
return i18nText[mylang] || '' return i18nText[mylang] || ''
} }
@ -69,9 +69,7 @@ export default {
}, },
localeText() { localeText() {
const pageNow = this?.$store ? this // 对于组件内定义的 i18nText要使用 this 来获得组件内的 i18nText而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 return thisPage().pageNow?.i18nText?.[getApp().$store.state.i18n.mylang] || {}
: getCurrentPages()[getCurrentPages().length - 1]
return pageNow?.i18nText?.[getApp().$store.state.i18n.mylang] || {}
}, },
setBarTitles ({ windowTitle, pageTitle } = {}) { setBarTitles ({ windowTitle, pageTitle } = {}) {
@ -437,8 +435,7 @@ export default {
}) })
*/ */
showToast({ tool, type, image, title, duration = 2000, ...rest }) { showToast({ tool, type, image, title, duration = 2000, ...rest }) {
const pageNow = this?.$store ? this // 对于组件内定义的 i18nText要使用 this 来获得组建内的 i18nText而不是 getCurrentPages[...] 去访问全局页面的 i18nText。 const pageNow = thisPage()
: getCurrentPages()[getCurrentPages().length - 1]
if (tool === 'uni' || ! pageNow?.$refs?.toast) { if (tool === 'uni' || ! pageNow?.$refs?.toast) {
// #ifdef APP-PLUS // #ifdef APP-PLUS
uni.showToast({ icon: 'none', title, duration, ...rest }) uni.showToast({ icon: 'none', title, duration, ...rest })