use getCurrentPages()?.pop?.() to replace this.thisPage?.()

This commit is contained in:
陆柯 2023-05-16 10:11:46 +08:00
parent 61424e89b6
commit c4d6b8ee79

View File

@ -3,6 +3,11 @@
import './ican-H5Api.js' // 对齐H5Api: https://ext.dcloud.net.cn/plugin?id=415 // 注意要取消默认自带的 showToast https://uniapp.dcloud.io/api/system/clipboard?id=%e6%b3%a8%e6%84%8f
const BASE_TYPE_DEFAULT = 'SERVER' // one of { SERVER: 服务器, UNICLOUD_FUNC: 云函数, UNICLOUD_OBJECT: 云对象 }
function langNow () {
// getCurrentPages() 在 topWindow/App.vue 里有可能为空,所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里getApp().$store.state 未定义,所以还是用 wo.ss
return wo?.ss?.i18n?.mylang || getApp()?.$store?.state?.i18n?.mylang
}
export default {
// 用直观的色彩,代替语义化的类型:
// uView 中大量组件都使用 type 来设置颜色,而 uni-ui 中只有少量组件用到。
@ -31,36 +36,36 @@ export default {
thisPage () {
return this.__page__
? this // constructor.name==='VueComponent' 只在 development 环境有用,在 production 环境会被简化成 'o'。
: getCurrentPages()[getCurrentPages().length - 1] || {} // [20220401] 发现在 topWindow 里, getCurrentPages 是 undefined。 // 在 App.vue 中调用 getCurrentPages() 返回的是空数组 [],因此在这里默认 {} 做保护。
: getCurrentPages().pop() || {} // [20220401] 发现在 topWindow 里,或者在 App.vue 里, getCurrentPages() 是 undefined 和 空数组 [],因此在这里默认 {} 做保护。
},
localizeText (i18nText) {
i18nText =
i18nText || // 如果传入i18n参数 ({zhCN:'...', enUS:'...'})
this.i18nText || // 1) 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。2) 对于组件内定义的 i18nText要使用 this 来获得组件内的 i18nText
this.thisPage?.()?.i18nText // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就报错了。因此通过 thisPage?.().i18nText 访问。
const mylang = wo?.ss?.i18n?.mylang // this.thisPage?.() 有可能为空(例如在 topWindow 里,或者在 App.vue 里),所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里getApp().$store.state 未定义
return i18nText?.[mylang] || (typeof i18nText === 'string' ? i18nText : '') // 必须检测是否string如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错
getCurrentPages()?.pop()?.i18nText // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就不存在了。因此通过 pageNow.i18nText 访问。
return i18nText?.[langNow()] || (typeof i18nText === 'string' ? i18nText : '') // 必须检测是否string如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错
},
localeText () {
// 专供绑定到 computed { lote: wo.localeText } 使用,这时 this 就是当前页面。
return this.i18nText?.[getApp().$store.state.i18n.mylang] || {}
return this.i18nText?.[langNow()] || {}
},
setBarTitles ({ windowTitle, pageTitle, pagesJson = this.pagesJson || wo?.pagesJson, envar = this.envar || wo?.envar } = {}) {
const mylang = getApp()?.$store?.state?.i18n?.mylang // 不要用 pageNow.$store防止在 App.vue 里无法获取当前页面。
const pageNow = this.thisPage?.() // 需要兼顾在 App.vue 时无法获取当前页面的情况,因为如果在 topWindow 里调用本函数getApp() 和 getCurrentPages()[getCurrentPages().length-1] 就是 undefined。
const langNow = langNow()
const pageNow = getCurrentPages()?.pop?.() // 需要兼顾在 App.vue 时无法获取当前页面的情况,因为如果在 topWindow 里调用本函数getApp() 和 getCurrentPages() 就是 undefined。
const navibarTitle =
pageTitle ||
pageNow?.i18nPageTitle?.[mylang] || // 页面.vue 的 i18nPageTitle 变量
pageNow?.i18nText?.[mylang]?.tPageTitle || // 页面.vue 的 i18nText 对象
pageNow?.i18nPageTitle?.[langNow] || // 页面.vue 的 i18nPageTitle 变量
pageNow?.i18nText?.[langNow]?.tPageTitle || // 页面.vue 的 i18nText 对象
pageNow?.pageTitle ||
pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[mylang] || // pages.json 的页面配置里
pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[langNow] || // pages.json 的页面配置里
''
windowTitle = windowTitle || wo?.envar?.callname?.[mylang] || pagesJson?.appInfo?.i18nText?.[mylang] || pagesJson?.globalStyle?.navigationBarTitleText || ''
windowTitle =
windowTitle || wo?.envar?.callname?.[langNow] || pagesJson?.appInfo?.i18nText?.[langNow] || pagesJson?.globalStyle?.navigationBarTitleText || ''
if (uni.getSystemInfoSync().deviceType === 'pc') {
uni.setNavigationBarTitle({ title: windowTitle + (navibarTitle ? ` - ${navibarTitle}` : '') })
@ -87,7 +92,7 @@ export default {
// 必须要在有 tab 的页面里 setTabBarItem 才有效果
//const midIndex = parseInt(pagesJson?.tabBar?.list?.length/2) // 如果存在midButton,实际上tabBar.list.length必须为偶数。不过为了心安再parseInt一下。
pagesJson?.tabBar?.list?.forEach((tab, tabIndex) => {
if (tab.i18nText && tab.i18nText[mylang]) {
if (tab.i18nText && tab.i18nText[langNow]) {
uni.setTabBarItem({
// #ifdef H5
index: tabIndex, // + ((pagesJson?.tabBar?.midButton?.iconPath && tabIndex >= midIndex)?1:0), // H5 里,如果使用了 midButtontabBarItem的index出现错位需hack调整。推测在H5里 midButton 作为一个普通tab被插入到 tabBar 里,导致 tabBar 的 index 和 pagesJson.tabBar.list 的 index 错位了。[20211031] 注意到,从 HBuilderX 3.2.12.20211029 起,在 H5 里也没有错位了。
@ -95,7 +100,7 @@ export default {
// #ifndef H5
index: tabIndex,
// #endif
text: tab.i18nText[mylang],
text: tab.i18nText[langNow],
})
}
})
@ -144,7 +149,7 @@ export default {
apiWhat = {},
timeout,
}) {
const thisRoute = this.thisPage?.()?.route || 'VueApp' // 立刻保存 this.thisPage?.().route因为在调用后台后可能已切换到了其他页面。
const thisRoute = getCurrentPages()?.pop?.()?.route || 'VueApp' // 立刻保存 route因为在调用后台后可能已切换到了其他页面。
const startTime = new Date().toJSON()
let url = undefined
let result = {}
@ -429,8 +434,8 @@ export default {
// rename to popToast?
if (tool !== 'uni') {
// 来自 <ucToast> 或 <u-toast> 或 <u-top-tips>
const toast = this.thisPage?.()?.$refs?.toast || getApp().globalData.toast || wo.toast
const popup = this.thisPage?.()?.$refs?.popup || getApp().globalData.popup || wo.popup
const toast = getCurrentPages()?.pop()?.$refs?.toast || getApp().globalData?.toast || wo.toast
const popup = getCurrentPages()?.pop()?.$refs?.popup || getApp().globalData?.popup || wo.popup
if (toast) {
toast.show({ type, title, duration, ...rest })
return
@ -565,7 +570,8 @@ export default {
}
},
next_focus (currentFocus, focusList = this.thisPage?.()?.focusList) {
next_focus (currentFocus, focusList) {
focusList = focusList || getCurrentPages?.pop()?.focusList
if (focusList) {
for (let n in focusList) {
focusList[n] = false