菜鸟看前端(微信小程序自定义组件封装-轮播图的封装)

在根目录下新建components文件夹

tip:所有文件都可以自定义名字
在这里插入图片描述

在components文件夹下新建swiper文件夹,在文件夹内新建component

在这里插入图片描述
新建完成后
在这里插入图片描述

  1. 在swiper.wxml中写入
<swiper indicator-dots='true' 
        indicator-active-color='#ff5777'
        autoplay='true'
        circular='true'
        interval='3000'
        class='swiper'>
  <block wx:for="{{images}}" wx:key="index">
    <swiper-item>
      <image class="swiper-image" src="{{item.image}}"/>
    </swiper-item>
  </block>
</swiper>

  1. 在swiper.js中写入
// components/w-swiper/w-swiper.js
Component({
  /**
   * 组件的属性列表
   */
   // 用来接收传入自定组件的数据
  properties: {
    images:{
      type:Array,
      value:[]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

  }
})
  1. 在swiper.wxss中写入
.swiper {
  height: 360rpx;
}

.swiper-image {
  width: 100%;
  height: 100%;
}
  1. 在swiper.json中写入
{
  "component": true,
  "usingComponents": {}
}

  1. 在父组件中json引入
{
  "usingComponents": {
    "van-button": "@vant/weapp/button/index",
    "drl-swiper":"../../components/swiper/swiper"
  }
}
  1. 父组件js中调用轮播图片
let app = getApp()
Page({

/**
 * 页面的初始数据
 */
data: {
  imgList:[]
},

/**
 * 生命周期函数--监听页面加载
 */
onLoad: function (options) {
  app.http.banner().then(res=>{
    console.log(res)
    let {list} = res.data.data.banner
    this.setData({
      imgList:list
    })
  })
},
.....
}
  1. 父组件的wxml中使用注册的组件,把轮播图片传递给自定义组件
<drl-swiper images="{{imgList}}"></drl-swiper>
轮播完成

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值