方法:在app.js的**onLaunch()**函数中调用wx.getMenuButtonBoundingClientRect方法,并加入到globalData中;在页面或组件中就可以使用app.globalData.xxx了。
// app.js
App({
onLaunch() {
let menuButtonObject = wx.getMenuButtonBoundingClientRect();
wx.getSystemInfo({
success: res => {
let navTop = menuButtonObject.top;
this.globalData.navTop = navTop; //胶囊按钮与顶部的距离
},
fail(err) {
console.log(err);
}
})
globalData: {},
})
页面/组件中使用
// index.js
const app = getApp(); // 获取app实例
Page({
data: {
nav_top: 0, // 接收数据并可以在wxml中使用
},
onShow() {
this.setData({
nav_top: app.globalData.navTop
})
})