const app = getApp(); Component({ properties: { title: { type: String, value: '' }, showBack: { type: Boolean, value: false }, // 拦截返回:为 true 时点击返回仅触发 bind:back 事件,由页面自行处理(如未保存草稿确认) interceptBack: { type: Boolean, value: false } }, data: { statusBarHeight: 20, // 运行期根据页面栈动态决定:只有确实存在上一页时才显示返回键, // 避免在底部 tab 根页(页面栈深度 = 1)误显示返回键 canBack: false }, lifetimes: { attached() { const h = (app.globalData && app.globalData.statusBarHeight) || 20; const pages = getCurrentPages(); this.setData({ statusBarHeight: h, canBack: pages.length > 1 }); } }, methods: { onBack() { const pages = getCurrentPages(); // 页面要求自行处理返回逻辑(典型场景:编辑页有未保存内容需确认) if (this.data.interceptBack) { this.triggerEvent('back'); return; } if (pages.length > 1) { wx.navigateBack({ delta: 1 }); } else { // 兜底:确实没有上一页时回到首页(正常 tab 根页不会显示返回键,此为保险) wx.reLaunch({ url: '/pages/index/index' }); } } } });