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

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

相关推荐
Dark_programmer1 小时前
钉钉小程序 - - - - - 小程序内跳转其他小程序
小程序·apache·钉钉
kyh10033811203 小时前
可商用去水印微信小程序源码(免费获取全部源码)
微信小程序·小程序·微信小游戏·去水印·去水印工具·微信去水印小程序
2501_915106324 小时前
H5 混合应用加密实践,从明文资源到安全 IPA 的多层防护体系
android·安全·ios·小程序·uni-app·iphone·webview
我命由我123455 小时前
微信小程序 - 页面返回并传递数据(使用事件通道、操作页面栈)
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
半开半落5 小时前
uniapp微信小程序端接收ai模型返回的SSE流式数据
微信小程序·小程序·uni-app·ai模型
2501_916007475 小时前
在 CICD 中实践 Fastlane + Appuploader 命令行,构建可复制的 iOS 自动化发布流程
android·运维·ios·小程序·uni-app·自动化·iphone
一匹电信狗6 小时前
【Linux我做主】进程实践:手动实现Shell
linux·运维·服务器·c++·ubuntu·小程序·开源
奺儿6 小时前
uniapp 小程序 报错 TypeError: Cannot convert undefined or null to object
小程序·uni-app
2501_915921436 小时前
从 HBuilder 到 App Store,uni-app 与 HBuilder 项目的 iOS 上架流程实战解析
android·ios·小程序·https·uni-app·iphone·webview
一只一只妖7 小时前
Taro+Vue3+TS+Vite 小程序开发底座搭建流程
vue.js·小程序·taro