wujingjing
2024-08-01 1ee0bcc741206079b645c584c578ee543ff75c35
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
45
46
47
48
49
50
51
52
53
54
55
56
57
// 导入封装的 接口 API 函数
import {
  reqCategoryData
} from '@/api/category'
 
Component({
  // 初始化数据
  data: {
    categoryList: [], // 商品分类列表数据
    activeIndex: 0 // 被激活那一项的索引,默认是 0
  },
 
 
  methods: {
    // 实现一级分类的切换效果
    updateActive(event) {
      // console.log(event.currentTarget.dataset)
      const {
        index
      } = event.currentTarget.dataset
 
      this.setData({
        activeIndex: index
      })
    },
 
    // 获取商品分类的数据
    async getCategoryData() {
      const res = await reqCategoryData()
 
      if (res.code === 200) {
        this.setData({
          categoryList: res.data
        })
      }
    },
    // 监听页面的加载
    onLoad() {
      // 调用获取商品分类的数据的方法
      this.getCategoryData()
    },
  },
 
 
 
 
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 1
        })
      }
    }
  }
})