// components/tabbar/tabbar.js const app = getApp(); Component({ options: { styleIsolation: 'apply-shared' // 页面样式影响组件,组件样式不影响页面 }, properties:{ active: { // 属性名 type: Number, value: 0 }, isScroll: { // 属性名 type: Boolean, value: false }, }, /** * 页面的初始数据 */ data: { num:0 // active:0, // isScroll:true, }, methods:{ handleTab(e){ let url = e.currentTarget.dataset.url console.log(1111111,url); wx.switchTab({ url, complete(e){ console.log(1111111,url,e); } }) }, /** * 回到顶部 */ backToTop: function () { wx.pageScrollTo({ scrollTop: 0 }) }, }, pageLifetimes: { show: function() { const that = this; // 2. 从本地取初始数量 const initNum = app.globalData.totalCartCount || 0; that.setData({ num: initNum }); // 3. 注册回调(用箭头函数,确保引用唯一,不会每次创建新函数) this.updateCartCallback = (newNum) => { console.log('updateCartCallback'); that.setData({ num: newNum }); app.globalData.totalCartCount = newNum; }; app.on('updateCartNum', this.updateCartCallback); }, hide: function() { // 只移除当前实例的回调,不影响其他页面的 TabBar 实例 if (this.updateCartCallback) { app.off('updateCartNum', this.updateCartCallback); } }, resize: function(size) { // 页面尺寸变化 } }, attached() { }, // 组件卸载时(极端情况兜底) detached() { if (this.updateCartCallback) { app.off('updateCartNum', this.updateCartCallback); } }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, })