reactjs-swiper 在react项目中实现轮播

在本文中,我们将通过在React项目中利用reactjs-swiper库,逐步介绍如何快速搭建轮播组件。首先,使用create-react-app初始化项目,接着安装必要的依赖包,包括reactjs-swiper和axios。然后,我们看到项目包含App.js、SwiperBar.jsx和navList.json等文件。最后,启动项目进行预览。

1.快速新建项目

npx create-react-app swiper

2.安装依赖

yarn add reactjs-swiper axios --save-dev

3.文件目录

App.js

import React, { Component } from 'react';
import './App.scss';
import SwiperBar from './SwiperBar'
class App extends Component {
  render() {
    return (
      <div>
        <SwiperBar />
      </div>
    );
  }
}

export default App;

SwiperBar.jsx

import ReactSwiper from 'reactjs-swiper';
import React,{ PureComponent } from 'react'
import axios from 'axios';
class ReactSwiperExample extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      swiperOptions: {
        preloadImages: true,
        autoplay: 1000,
        showPagination: true,
        autoplayDisableOnInteraction: false
      },
      items:[]
    }
  }
  componentDidMount() {
    axios.get('/api/navList.json').then( (response) => {
      console.log(response.data.data);
      this.setState({
        items: response.data.data
      })
    }).catch(e => (console.log(e)))
  }
  render() {
    return (
      <div>
        <ReactSwiper swiperOptions={this.state.swiperOptions} showPagination items={this.state.items} className="swiper-example" >
          
        </ReactSwiper>
        <div>
          {
            this.state.items.map((item) => {
              return(
                <div key={item.id}>
                  <img src={item.image} alt=""/>
                </div>
              )
              
            })
          }
        </div>
      </div>
    )
  }
}

export default ReactSwiperExample;  

navList.json

{
  "success": true,
  "data": [
    {
      "id": 1,
      "image": "https://upload.jianshu.io/admin_banners/web_images/4550/a4aba36241eb146c13892301e486417cbf16af18.png?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540",
      "title": "图片1" 
    },
    {
      "id": 2,
      "image": "https://upload.jianshu.io/admin_banners/web_images/4573/8e236c77ce2141beeaee6515fb3abddfbb617fea.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540",
      "title": "图片2" 
    },
    {
      "id": 3,
      "image": "https://upload.jianshu.io/admin_banners/web_images/4566/867d2e49d573864ec5ae4ed9e6578a254a0c991d.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540",
      "title": "图片3" 
    },
    {
      "id": 4,
      "image": "https://upload.jianshu.io/admin_banners/web_images/4570/5d4776585f0206ff2e807971a13b06ed7d494595.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540",
      "title": "图片4" 
    },
    {
      "id": 5,
      "image": "https://upload.jianshu.io/admin_banners/web_images/4574/d5c5ea3e984c8c08071b467c2dfe5bd2d0acf7b8.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540",
      "title": "图片5" 
    },
    {
      "id": 6,
      "image": "https://upload.jianshu.io/admin_banners/web_images/4564/563e90e1ea09698e8a9ad1a9c4fb36bcea2655be.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540",
      "title": "图片6" 
    }
  ]
}

yarn start

The End

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值