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

在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,无论调用的时候用什么值,都会显示指示器,而其他属性改变了是可以改变轮播图的运行的,如果有哪位刚好知道,请告知解惑,谢谢!

相关推荐
黄华SJ520it3 小时前
139小程序商城模式开发
小程序·软件需求·系统开发
Greg_Zhong3 小时前
详细说下小程序中使用canvas的体验
小程序·canvas绘制·canvas绘制内容溢出·绘制内容模拟器不正常·绘制内容真机正常
小羊Yveesss6 小时前
2026 多门店小程序如何提升效率?连锁门店降本增效实操指南,数字化转型必看
大数据·小程序
2501_941982056 小时前
提高私域转化率:如何通过 API 自动发送小程序卡片?
小程序·机器人·自动化·企业微信·rpa
码视野10 小时前
完全开源-支持二开-可做毕业论文-家政服务预约小程序
小程序
码视野10 小时前
全开源-健身运动预约小程序 — 从需求到原型的全栈实践
小程序
游戏开发爱好者811 小时前
深入理解iOSTime Profiler:提升iOS应用性能的关键工具
android·ios·小程序·https·uni-app·iphone·webview
tianxiaxue111 小时前
微信小程序如何跟企微互通
微信小程序·小程序·企业微信
Greg_Zhong11 小时前
微信小程序中canvas绘制面积图,解决手机和模拟器都能渲染不溢出问题
微信小程序·小程序canvas绘制面积图
小小王app小程序开发12 小时前
潮玩抽赏盲盒小程序玩法分析:2026 潮玩风口,技术合规双驱动
小程序