index.js
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Component({
properties: {
// 导航栏标题
title: {
type: String,
value: ''
},
// 背景颜色
background: {
type: String,
value: 'transparent'
},
// 标题颜色
color: {
type: String,
value: '#333333'
},
// 是否显示返回按钮
showBack: {
type: Boolean,
value: true
},
// 返回按钮图标颜色
backIconColor: {
type: String,
value: '#ffffff'
},
// 是否显示首页按钮
showHome: {
type: Boolean,
value: false
},
// 特殊模式,不占高度
noPlaceholder: {
type: Boolean,
value: false
}
},
data: {
statusBarHeight: 0,
navBarHeight: 44,
totalHeight: 0
},
lifetimes: {
attached() {
const app = getApp()
// 直接使用 app.globalData 中已经计算好的数据
this.setData({
statusBarHeight: app.globalData.statusBarHeight,
navBarHeight: app.globalData.navBarHeight,
totalHeight: app.globalData.totalNavHeight
})
}
},
methods: {
// 返回上一页
onBack() {
const pages = getCurrentPages()
if (pages.length > 1) {
wx.navigateBack({
delta: 1
})
} else {
// 如果是第一个页面,尝试返回首页
wx.switchTab({
url: '/pages/index/index',
fail: () => {
wx.reLaunch({
url: '/pages/index/index'
})
}
})
}
this.triggerEvent('back')
},
// 返回首页
onHome() {
wx.switchTab({
url: '/pages/index/index',
fail: () => {
wx.reLaunch({
url: '/pages/index/index'
})
}
})
this.triggerEvent('home')
}
}
})