Axios请求与Vue遍历

一、页面效果

在这里插入图片描述



二、接口请求

思维:
1、引用axios
2、创建变量,存放数据
3、创建一个函数(页面挂载好之后执行)
4、请求POST接口
5、获得请求数据
6、创建声明周期钩子函数, 页面挂载好之后执行 4

<script>
import axios from 'axios'

export default {
  name: 'IndexCourses',
  data () {
    return {
      userList:[]
    }
  },
  methods: {
    // 创建一个函数
    getCoursesInfo () {
      // 通过axios进行数据请求
      axios({
        method: 'post',
        url: 'http://192.168.0.110:8888/coach/findGoldCoach?currentPage=1&pageSize=4',
      }).then((res)=>{
        // console.log(res.data.data)
        this.userList = res.data.data
      })
    }
  },
  // 创建一个生命周期函数,当页面挂载好了之后,执行这个函数
  mounted () {
    this.getCoursesInfo()
  }
}
</script>


三、遍历

通过v-for、{{}}将获取的数据进行遍历展示

<template>
  <div class="courses">
    <div class="courses-nav">
      <div class="courses-sec">
        <h2 class="sec-title">FITNESS金牌健身教练</h2>
        <p class="sec-describe">舒适的健身环境、多功能操房、团体私教工作室、50+专业资深教练组成的研发团队打造的爱动原创课程、国际版权课程以及完善的客户服务满足您运动前后的所有需求,每处细节彰显FITNESS爱动课程体验中心“服务至上”的理念。</p>
      </div>
      <div class="courses-content">
        <ul class="courses-list">
          <div class="list-wrapper">
            <router-link to="/instructor/instructorDetails">
              <li class="courses-item" v-for="(item, index) in this.userList" :key="index">
                <div class="item-top">
                  <img class="item-top-img" :src="item.coachPhoto" alt="">
                </div>
                <div class="item-bottom">
                  <h3 class="item-title">{{item.coachClass}}  {{item.coachName}}</h3>
                  <p class="item-describe">擅长: {{item.coachSpecialty}}</p>
                </div>
              </li>
            </router-link>
          </div>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'IndexCourses',
  data () {
    return {
      userList:[]
    }
  },
  methods: {
    // 创建一个函数
    getCoursesInfo () {
      // 通过axios进行数据请求
      axios({
        method: 'post',
        url: 'http://192.168.0.110:8888/coach/findGoldCoach?currentPage=1&pageSize=4',
      }).then((res)=>{
        // console.log(res.data.data)
        this.userList = res.data.data
      })
    }
  },
  // 创建一个生命周期函数,当页面挂载好了之后,执行这个函数
  mounted () {
    this.getCoursesInfo()
  }
}
</script>

<style lang="stylus" scoped>
  .courses
    width 1920px
    height 800px
    background-color #e9eff4
    padding-top 115px
    .courses-nav
      width 1200px
      height 100%
      margin 0 352.5px
      padding 0 15px
      // background #f5
      .courses-sec
        height 136px
        padding 0 170px
        color #595959
        font-family montserrat,sans-serif
        .sec-title
          height 52px
          line-height 52px
          font-family Arial
          font-size 48px
          text-align center
        .sec-describe
          margin-top 15px
          height 52px
          line-height 22px
          font-size 16px
          font-family sans-serif
          text-align center
      .courses-content
        margin-top 35px
        height 424px
        display flex
        // background #000
        .list-wrapper
          float left
          margin 15px
          .courses-item
            float left
            overflow hidden
            box-sizing border-box
            width 270px
            height 329px
            border-radius 10px
            background-color #04d2c8
            margin-left 20px
            margin-right auto
            .item-top
              width 270px
              height 220px
              .item-top-img
                width 270px
                height 220px
                overflow hidden
            .item-bottom
              padding 30px 15px
              color #fff
              .item-title
                height 22px
                line-height 22px
                font-size 18px
                text-align center
                font-weight 500
                font-family montserrat,sans-serif
              .item-describe
                margin-top 10px
                height 22px
                line-height 22px
                font-size 14px
                font-weight 300
                text-align center           
</style>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后海 0_o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值