减少App.vue的代码依赖,优化获取顶部自定义导航栏的高度信息

This commit is contained in:
JaylenTech
2021-12-31 09:43:37 +08:00
parent 61b53bdafc
commit e89b56d3ce
3 changed files with 29 additions and 87 deletions

View File

@@ -175,16 +175,7 @@
},
mounted() {
// 获取vuex中的自定义顶栏的高度
let customBarHeight = this.vuex_custom_bar_height
let statusBarHeight = this.vuex_status_bar_height
// 如果获取失败则重新获取
if (!customBarHeight) {
this.$t.updateCustomBar()
customBarHeight = this.vuex_custom_bar_height
statusBarHeight = this.vuex_status_bar_height
}
this.customBarHeight = customBarHeight
this.statusBarHeight = statusBarHeight
this.updateNavBarInfo()
},
created() {
// 获取胶囊信息
@@ -196,6 +187,32 @@
// #endif
},
methods: {
// 更新导航栏的高度
async updateNavBarInfo() {
// 获取vuex中的自定义顶栏的高度
let customBarHeight = this.vuex_custom_bar_height
let statusBarHeight = this.vuex_status_bar_height
// 如果获取失败则重新获取
if (!customBarHeight) {
try {
const navBarInfo = await this.$t.updateCustomBar()
customBarHeight = navBarInfo.customBarHeight
statusBarHeight = navBarInfo.statusBarHeight
} catch(e) {
setTimeout(() => {
this.updateNavBarInfo()
}, 10)
return
}
}
// 更新vuex中的导航栏信息
this && this.$t.vuex('vuex_status_bar_height', statusBarHeight)
this && this.$t.vuex('vuex_custom_bar_height', customBarHeight)
this.customBarHeight = customBarHeight
this.statusBarHeight = statusBarHeight
},
// 处理返回事件
async handlerBack() {
if (this.beforeBack && typeof(this.beforeBack) === 'function') {

View File

@@ -1,8 +1,8 @@
/**
* 更新自定义顶部导航栏的高度
*/
async function updateCustomBarInfo () {
return await new Promise((resolve, reject) => {
function updateCustomBarInfo () {
return new Promise((resolve, reject) => {
uni.getSystemInfo({
success: (e) => {
let statusBarHeight = 0
@@ -27,8 +27,6 @@ async function updateCustomBarInfo () {
statusBarHeight = e.statusBarHeight
customBarHeight = e.statusBarHeight + e.titleBarHeight
// #endif
this && this.$t.vuex('vuex_status_bar_height', statusBarHeight)
this && this.$t.vuex('vuex_custom_bar_height', customBarHeight)
resolve({
statusBarHeight,
customBarHeight
@@ -36,8 +34,6 @@ async function updateCustomBarInfo () {
},
fail: (err) => {
console.log("获取设备信息失败", err);
this && this.$t.vuex('vuex_status_bar_height', 0)
this && this.$t.vuex('vuex_custom_bar_height', 0)
reject()
}
})