老莫的笔记本  
  
查看: 1065|回复: 0

umi 或者 react 中 如何使用 swiper

[复制链接]

662

主题

878

帖子

5143

积分

超级版主

Rank: 8Rank: 8

积分
5143
发表于 2020-10-22 10:52:38 | 显示全部楼层 |阅读模式
需求明确:需要在react 中使用 swiper ,且使用导航,自动播放等功能。

参考文献:https://swiperjs.com/react/   [  这是swiper 的 方式 ];

前提条件:
    1. umi 默认使用的是css modules , 如果要使用 react 的 css 引入方式, 需要在 umirc.ts 文件里 增加配置[ 参照上篇或者百度 哈哈  我当时可是给这个憋了三四天才明白 不得不说 umi 的官方文档 莫名其妙 呵呵]。
    2. swiper 的官方文档引入的是 .sass 文件 , umi 不具备编辑此文件的 能力, 全部换成 .less 结尾即可
    3. 建议使用 cnpm 的 淘宝镜像源 进行安装 否则会凉

开始:
    1. 在react 中安装swiper

  1. cnpm  install swiper --save
复制代码

    2. 在 react 的 项目中 引入文件 [这是最基础的swiper , 没有任何组件]
         [ 这里得说明,该引入 是最初始化的 不包含 组件 ]  
  1. import { Swiper, SwiperSlide } from 'swiper/react';
  2. import "../../assets/style/laomo.css";
  3. import 'swiper/swiper.less';



  4. <Swiper
  5.                     spaceBetween={10}
  6.                     slidesPerView={4}
  7.                     zoom={true}
  8.                     loop={true}  // 这个不用组件可以直接用
  9.            
  10.                     pagination={{ clickable: true }}
  11.                     onSlideChange={() => console.log('slide change')}
  12.                     onSwiper={(swiper) => console.log(swiper)}
  13.                      className="swiper-container">

  14.                     {
  15.                         this.state.hezuoArr.map((item, index)=>{
  16.                             return (
  17.                                 <SwiperSlide key={index} className="tbox">
  18.                                     <div className="tbox-img">
  19.                                         <img src={item.img}/>
  20.                                     </div>
  21.                                     <div className="tbox-title">
  22.                                         {item.title}
  23.                                     </div>
  24.                                 </SwiperSlide>
  25.                             );
  26.                         })
  27.                     }
  28.                     
  29.                   
  30.                 </Swiper>
复制代码



    3.  你得用组件吧,然后 就引入

  1. import SwiperCore, { Navigation, Pagination, Autoplay} from 'swiper';//这里 只做个样子
  2. import 'swiper/components/navigation/navigation.less';//这里 只做个样子
  3. import 'swiper/components/pagination/pagination.less';//这里 只做个样子
  4. import 'swiper/components/scrollbar/scrollbar.less'; //这里 只做个样子
  5. SwiperCore.use([Navigation ,Autoplay]);//这里 只做个样子
复制代码
(每种 组件的使用方法不同, 就比方说 有的有less  有的没有, 有的 直接就能用 ,有的需要加在属性里:例如 autopaly 就得引入且加在属性里)

最终版
  1. // 这是合作伙伴栏目
  2. import React from 'react';
  3. import createReactClass from 'create-react-class';  
  4. import SwiperCore, { Autoplay} from 'swiper';
  5. import { Swiper, SwiperSlide } from 'swiper/react';
  6. import "../../assets/style/laomo.css";
  7. import 'swiper/swiper.less';


  8. const Cooperation = createReactClass({

  9.     getInitialState:function(){
  10.         return {
  11.             hezuoArr:[
  12.                 {'title':'合作伙伴1','img':'http://www.lygky.com/img/logos/logo.png'},
  13.                 {'title':'合作伙伴2','img':'http://www.lygky.com/img/logos/logo.png'},
  14.                 {'title':'合作伙伴3','img':'http://www.lygky.com/img/logos/logo.png'},
  15.                 {'title':'合作伙伴4','img':'http://www.lygky.com/img/logos/logo.png'},
  16.                 {'title':'合作伙伴5','img':'http://www.lygky.com/img/logos/logo.png'},
  17.                 {'title':'合作伙伴6','img':'http://www.lygky.com/img/logos/logo.png'},
  18.                 {'title':'合作伙伴7','img':'http://www.lygky.com/img/logos/logo.png'},
  19.                 {'title':'合作伙伴8','img':'http://www.lygky.com/img/logos/logo.png'}
  20.             ]
  21.         }
  22.     },

  23.     render() {
  24.         SwiperCore.use([Autoplay]);
  25.         return (
  26.             <section className="hezuo-box">
  27.                <div  className="min-section">  
  28.                     <div className="min-title"> - 公司案例与合作伙伴 - </div>
  29.                     <p className="min-title-p1">为每一位使用者守住您的知识产权与客户档案</p>
  30.                 </div>
  31.                 <div className="ttt">
  32.                 <Swiper
  33.                     spaceBetween={10}
  34.                     slidesPerView={4}
  35.                     zoom={true}
  36.                     loop={true}
  37.                     autoplay = {true}
  38.                     pagination={{ clickable: true }}
  39.                     onSlideChange={() => console.log('slide change')}
  40.                     onSwiper={(swiper) => console.log(swiper)}
  41.                      className="swiper-container">

  42.                     {
  43.                         this.state.hezuoArr.map((item, index)=>{
  44.                             return (
  45.                                 <SwiperSlide key={index} className="tbox">
  46.                                     <div className="tbox-img">
  47.                                         <img src={item.img}/>
  48.                                     </div>
  49.                                     <div className="tbox-title">
  50.                                         {item.title}
  51.                                     </div>
  52.                                 </SwiperSlide>
  53.                             );
  54.                         })
  55.                     }
  56.                     
  57.                   
  58.                 </Swiper>
  59.                 </div>
  60.                
  61.             </section>
  62.         );
  63.          
  64.     }
  65. })

  66. export default Cooperation;
复制代码






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表