const LIST = [ { pagePath: '/pages/home/home', text: '首页', icon: 'home' }, { pagePath: '/pages/record/record', text: '记录', icon: 'record' }, { pagePath: '/pages/report/report', text: '报告', icon: 'report' }, { pagePath: '/pages/community/community', text: '社区', icon: 'community' }, { pagePath: '/pages/mine/mine', text: '我的', icon: 'user' }, ]; Component({ data: { selected: 0, hidden: false, list: LIST }, // 高亮由当前页面路径自己算出来。 // 原来每个 tab 页在 onShow 里硬写 selected: 0/1/2/3/4,调一次顺序就要同时改 // 五个文件,漏一个就高亮错位——这次把计划换成我的正好会踩到。 pageLifetimes: { show() { this.syncSelected(); }, }, lifetimes: { attached() { this.syncSelected(); }, }, methods: { syncSelected() { const pages = getCurrentPages(); const cur = pages[pages.length - 1]; if (!cur) return; const path = '/' + cur.route; const i = LIST.findIndex((t) => t.pagePath === path); if (i >= 0 && i !== this.data.selected) this.setData({ selected: i }); }, switchTab(e) { wx.switchTab({ url: LIST[e.currentTarget.dataset.index].pagePath }); }, }, });