list.vue 2.1 KB
Newer Older
sin's avatar
sin committed
1 2
<template>
    <div class="product-list">
3 4 5 6 7 8 9
        <van-nav-bar :title="rootCategory.name" left-text="返回" left-arrow
                     @click-left="onBack">
        </van-nav-bar>

        <van-tabs v-model="active" @click="onCategoryClick">
            <van-tab v-for="category in childCategories" :title="category.name"  />
        </van-tabs>
sin's avatar
sin committed
10 11 12 13 14 15 16 17

        <div v-for="(product,i) in products" :key="i">
          <product-card :product='product' @click="showProduct(product)" />
        </div>
    </div>
</template>

<script>
18
import { getProductCategoryList, getProductSpuPage } from '../../api/product';
sin's avatar
sin committed
19 20 21 22

export default {
  data() {
    return {
23 24 25 26 27 28 29 30 31 32
      rootCategory: {
        id: parseInt(this.$route.query.cidFirst),
        name: this.$route.query.title,
      },
      childCategory: {
        id: parseInt(this.$route.query.cidSecond),
      },
      childCategories: [],
      active: 2,
      products: [],
sin's avatar
sin committed
33 34 35
    };
  },
  methods: {
36 37 38 39 40 41 42 43 44
    onBack() {
      history.back();
    },
    onCategoryClick(key) {
      // 设置激活的 key
      this.active = key;
      // 加载商品
      this.products = [];
      this.loadProductList(this.childCategories[key].id);
sin's avatar
sin committed
45
    },
46 47 48 49 50 51 52 53 54
    loadProductList(categoryId) {
      // 设置当前选中的分类
      this.childCategory.id = categoryId;
      // 读取商品
      // alert('商品分类:' + categoryId);
      let response = getProductSpuPage(categoryId);
      response.then(data => {
        this.products.push(...data.spus);
      })
sin's avatar
sin committed
55
    }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  },
  mounted() {
    let response = getProductCategoryList(this.rootCategory.id);
    response.then(data => {
      // console.log(data);
      // debugger;
      // 设置根节点的分类
      this.childCategories = data;
      // 设置激活的分类
      // debugger;
      for (let i = 0; i < data.length; i++) {
        if (data[i].id === this.childCategory.id) {
          this.active = i;
          break;
        }
      }
      // 加载商品列表
      this.loadProductList(this.childCategory.id);
    });
sin's avatar
sin committed
75 76 77 78 79 80 81 82 83 84 85 86 87
  }
};
</script>

<style lang="less">
.product-list{
    .additional .price{
        position: absolute;
        bottom: 6px;
        height: 34px;
    }
}
</style>