vue2.x基于vant组件库开发交流圈子系列一(列表页面)

<template>
  <div>
    <!-- tab -->
    <van-sticky>
      <van-tabs v-model="active" color='#00beca' line-width='20px' @change="changeType">
        <van-tab name='' title="全部"></van-tab>
        <van-tab v-for="(item,index) in menuData" :key="index" :name='item.id' :title="item.category_name"></van-tab>
      </van-tabs>
    </van-sticky>
    <!-- 圈子列表 -->
    <div class="warp">
      <van-list v-model="loading" :finished="finished" :immediate-check="false" finished-text="没有更多了" @load="onLoad" :offset="10">
        <div v-for="(item,index) in list" :key="index" class="content" @click="toPage(item.id)">
          <van-cell :icon="item.group_thumb" :border='false'>
            <template #default>
              <van-button type="info" round size="mini" icon="plus" color="#00beca" v-if="item.is_join==0" @click.stop="addcommunity(item.id)">加入
              </van-button>
              <van-tag type="primary" size="medium" round plain v-if="item.is_join==1">已加入</van-tag>
            </template>
            <template #title>
              {{item.group_name}}
            </template>
            <template #label>
              管理员 大王的侄女 · 成员 {{item.group_number}}
            </template>
          </van-cell>
          <div class="desc">
            [42323条动态] 妙手生花:一生狂魔,一世狂魔 生而为人...
            <van-divider />
          </div>
        </div>
        <div class="loading" style="background-color: #fff;text-align: center;padding-top: 20px;" v-if="list==''">
          <van-loading size="20px" type="spinner">请稍等...</van-loading>
        </div>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getcommunityCategory, getcommunityGroup, addcommunity } from "@/api/community";
export default {
  data() {
    return {
      active: 0,
      menuData: [],//圈子分类
      list: [],//圈子列表
      category_id: 0,//分类id
      page: 1,
      page_size: 10,
      total: 0,//总共的数据条数
      loading: false,
      finished: false,
    }
  },
  methods: {
    // 获取圈子分类
    async getcommunityCategory() {
      try {
        const res = await getcommunityCategory()
        this.menuData = res.data.list
        this.getcommunityGroup(this.page, this.page_size, this.category_id)
      } catch (error) {
        console.log(error);
      }
    },
    // 获取圈子列表
    async getcommunityGroup(page, page_size, category_id) {
      try {
        const res = await getcommunityGroup({ page: page, page_size: page_size, category_id: category_id })
        this.list = res.data.list
      } catch (error) {
        console.log(error);
      }
    },
    // 切换tab触发
    changeType(name, title) {
      this.category_id = name
      this.page = 1
      this.loading = false
      this.finished = false
      this.getcommunityGroup(this.page, this.page_size, this.category_id)
    },
    // 上拉加载
    async onLoad() {
      try {
        this.page++
        const res = await getcommunityGroup({ page: this.page, page_size: this.page_size, category_id: this.category_id })
        let rows = res.data.list
        this.loading = false;
        this.total = res.data.count;
        if (rows == null || rows.length === 0) {
          // 加载结束
          this.finished = true;
          return;
        }
        // 将新数据与老数据进行合并
        this.list = this.list.concat(rows);
        //如果列表数据条数>=总条数,不再触发滚动加载
        if (this.list.length >= this.total) {
          this.finished = true;
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 加入圈子
    async addcommunity(id) {
      try {
        const res = await addcommunity(id)
        this.$dialog.alert({
          title: '温馨提示',
          message: '加入成功',
          theme: 'round-button',
        }).then(() => {
          // on close
          this.getcommunityGroup({ page: this.page, page_size: this.page_size, category_id: this.category_id })
        });
      } catch (error) {
        this.$toast.fail(error.message)
      }
    },
    toPage(index) {
      this.$router.push('/community/communityDetail/' + index)
    },
  },
  mounted() {
    this.getcommunityCategory()
  }
}
</script>
<style lang="scss" scoped>
.warp {
  padding: 10px 10px 0 10px;
  box-sizing: border-box;
  ::v-deep .van-cell {
    padding: 10px 0;
    .van-cell__left-icon {
      width: 40px;
      height: 40px;
      margin-right: 10px;
      .van-icon__image {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        object-fit: cover;
      }
    }
    .van-button--mini {
      padding: 0 8px;
    }
    .van-cell__label {
      white-space: nowrap;
      font-weight: normal;
      line-height: 15px;
    }
    .van-cell__title {
      font-weight: bold;
      font-size: 15px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      flex: 1.3;
    }
  }
  .desc {
    padding-left: 50px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: #777;
    font-size: 12px;
  }
}
</style>

推荐阅读更多精彩内容