主页(四)-轮播图——Carousel 走马灯组件 & axios-配置请求基准路径和配置响应拦截器

主页(四)-轮播图——Carousel 走马灯组件 & axios-配置请求基准路径和配置响应拦截器

Carousel 走马灯组件:https://mobile.ant.design/components/carousel-cn/

  • 基本布局配置
<Carousel autoplay={this.state.isLoaded} infinite>
  {this.state.swipers.map(item => (
    <a
      key={item.id}
      href="http://www.itcast.com"
      style={{
        display: 'inline-block',
        width: '100%',
        height: this.state.imgHeight
      }}
    >
      <img
        src={`http://localhost:8080${item.imgSrc}`}
        alt="图片无法显示"
        style={{ width: '100%', verticalAlign: 'top' }}
        onLoad={() => {
          // 窗口尺寸变化时->触发resize事件->重新设置动态高
          window.dispatchEvent(new Event('resize'))
          this.setState({ imgHeight: 'auto', isLoaded: true })
        }}
      />
    </a>
  ))}
</Carousel>

静态轮播图

在这里插入图片描述

  • 对接后台接口数据
    • 安装axios包
      • yarn add axios
    • 导入axios
      • import axios from ‘axios’

index.js中

  // 轮播图数据加载
  loadSwiper = async () => {
    let res = await axios.get('http://localhost:8080/home/swiper')
    this.setState({
      swiper: res.data.body
    })
  }
  componentDidMount () {
    this.loadData()
  }

App.js中

import axios from 'axios'
// 配置请求基准路径
axios.defaults.baseURL = 'http://localhost:8080/'
// 配置响应拦截器
axios.interceptors.response.use((res) => {
  return res.data
})

最终效果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值