Component({ data: { active: 0, list: [ { pagePath: "/pages/index/index", text: "首页", iconName: "ic-home" }, { pagePath: "/pages/message/message", text: "消息", iconName: "ic-comment" }, { pagePath: "/pages/profile/profile", text: "我的", iconName: "ic-me" } ] }, attached() { this.init(); }, methods: { switchTab(event) { const index = event.currentTarget.dataset.index; this.setData({ active: index }); wx.switchTab({ url: this.data.list[index].pagePath }); }, init() { const page = getCurrentPages().pop(); const route = page ? page.route : ''; const active = this.data.list.findIndex( (item) => item.pagePath === `/${route}` ); this.setData({ active }); } } });