lrj
6 小时以前 29fc6f5b1981775be5d2f0f9f8e61fec2f550252
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 });
    }
  }
});