微信小程序之将轮播图设计为组件

在components文件夹上点右键,新建component,命名为swiper

然后将我们之前的代码都拷贝到对应文件中,

然后我们的页面要引用这个组件,

在pages\index\index.json中引入:

复制代码
{  
  "usingComponents": {
    "van-search": "@vant/weapp/search/index",
    "my-swiper":"../../components/swiper/swiper"
  }
}

在index.wxml中使用

复制代码
<view class="index-container">
  <view class="header">
    <van-search value="{{ value }}" shape="round" background="#fa2c19" placeholder="请输入搜索关键词" />
    <my-swiper
      circular="{{swiperOption.circular}}"
      autoplay="{{swiperOption.autoplay}}"
      interval="{{swiperOption.interval}}"
      duration="{{swiperOption.duration}}"
      indicator-dots="{{swiperOption.indicatorDot}}"
    > 
    </my-swiper>
  </view>
</view>

pages\index\index.js 中数据

复制代码
Page({
  data: {
      value: '',
      swiperOption: {
        indicatorDot: true,
        autoplay: true,
        interval: 3000,
        circular: true,
        duration: 1000,
        indicatorColor:'#fff',
        indicatorActiveColor:'#fa2c19'
      }    
  },
  
})

pages\index\index.wxss 中之前swiper和image的样式要删除掉

复制代码
.header {
  background-image: -webkit-linear-gradient(botton, #f1503B, #c82519 50%);
  background-image: linear-gradient(0deg, #f1503b, #c82519 50%);
  width: 100%;
  height: 190px;
  border-bottom-left-radius: 100%;
  border-bottom-right-radius: 100%;
}

这样就好了!!

意外:

我发现indicator-dots的属性值传递不下去,组件中设置了false,无论调用的时候用什么值,轮播图指示器都不显示,同样,组件中indicator-dots属性设置了true,无论调用的时候用什么值,都会显示指示器,而其他属性改变了是可以改变轮播图的运行的,如果有哪位刚好知道,请告知解惑,谢谢!

相关推荐
2601_9623446210 小时前
计算机毕业设计之基于大数据的投保数据的分析系统的设计与实现
大数据·人工智能·深度学习·机器学习·信息可视化·小程序·课程设计
黑黑的独立开发笔记14 小时前
「 简记往来」第十五篇:小程序性能优化——首屏从2.5秒到1.2秒
性能优化·小程序·首屏优化·分包加载·setdata·简记往来
tcdos3 天前
不止扫码 — 微信生态深度融合(登录 + 支付 + 消息)
后端·微信小程序
小徐_23333 天前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
蜗牛前端6 天前
codex 全流程开发上线的高颜值礼簿小程序
前端·微信小程序
爱勇宝9 天前
我想认真做一件小事:让孩子和家长更好地互动
微信小程序·小程序·云开发
唯火锅不可辜负9 天前
避坑指南:iOS 下 scroll-view 嵌套 fixed 布局的“翻车”现场与修复
微信小程序
didiplus10 天前
运维人的随身神器:我把25个常用工具塞进了微信小程序
微信小程序
一份执念10 天前
uni-app 小程序分包限制处理与主包体积优化实战
前端·微信小程序
一份执念10 天前
ECharts 安装与使用完全指南:从全量引入到小程序分包优化
微信小程序·echarts