微信小程序进阶-自定义组件

本文详细介绍了如何在微信小程序中创建自定义组件,包括文件结构(json、xml、wxss、js)、组件的配置和使用方法。通过实例展示了如何编写json、xml和js文件,以及如何在页面中引用和配置自定义组件。
摘要由CSDN通过智能技术生成

引言

随着小程序越来越像APP,自带的组件和第三方的一些组件不能满足当前的业务需求,这时就需要我们自己去配置可以重复利用的组件。

自定义组件

组件的创建

1. 在项目的根目录创建components文件夹(主要是为了项目结构清晰);
2. 在上面的文件夹里再创建一个文件夹,文件夹的名字就是组件的名字;
3. 选中组件的文件夹,右键选择Component并点击;
4. 在组件文件夹里会自动生成4个文件,和页面的文件一样,具体看下面的frame组件。

在这里插入图片描述

组件文件结构

json文件

1.配置为组件
"component": true
2.引用其他组件
"usingComponents": {}

xml文件

配置组件的布局,具体看下面:
<view class="parent">
  <!-- 整体框架的背景 -->
  <view class="header_bg">
    <image class="top-image" mode="widthFix" src="../images/wd_bg.png" />
  </view>
  <!-- wrap -->
  <view class="content-wrap">
    <!-- 配置状态栏 -->
    <view class="top-bar {{position == 0 ? '' : 'top-bar-title-middle'}}" style="margin-top: {{marginTop}}px;">
      <image wx:if="{{visiable}}" class="top-bar-image" src="../images/top_return.png" mode="aspectFit" bindtap="onClick"/>
      <view class="title {{position == 0 ? 'top-bar-title-left' : ''}}">{{title}}</view>
      <!-- 右边的无效的view,为了标题居中 -->
      <view wx:if="{{visiable}}" class="top-bar-image top-bar-image-right" />
    </view>
      
    <!-- content -->
    <slot></slot>
  </view>
</view>

wxss文件

配置组件的样式,具体看下面:
.parent {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}
.title {
  /* width: 100%; */
  height: 70rpx;
  font-size: var(--fs42);
  font-weight: 500;
  /* color: var(--defaultColor); */
  color: #fff;
  /* margin-top: 80rpx; */
  /* position: relative; */
  text-align: center;
  line-height: 70rpx;
}
.header_bg {
  width: 100vw;
  height: 583rpx;
  /* height: 100%; */
  position: absolute;
}
.top-image {
  width: 100%;
  height: 100%;
}
.content-wrap {
  position: relative;
  width: 100%;
}

.top-bar {
  position: relative;
  display: flex;
  flex: column;
  /* justify-items: center; */
  /* 垂直居中 */
  align-items: center; 
  width: 100%;
  /* height: 70rpx; */
  height: 32px;
  /* background-color: brown; */
}
.top-bar-title-left {
  margin-left: 30rpx;
}
.top-bar-title-middle {
  justify-content: center;
}

.top-bar-image {
  width: 34rpx;
  height: 34rpx;
  margin-left: 20rpx;
}

.top-bar-image-right {
  margin-right: 20rpx;
  /* background-color: chartreuse; */
}

js文件

组件的逻辑处理,具体看下面:
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    /**
     * 标题栏标题的内容,不显示可以不设置值。
     */
    title: {
      type: String,
      value: ''
    },
    /** 返回按钮是否可见 */
    visiable: {
      type: Boolean,
      value: true
    },
    /**
     * 标题的位置,0:靠左;其他值居中
     */
    position: {
      type: Number,
      value: 0
    },
    distributed: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    marginTop: getApp().globalData.marginTop
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClick() {
      console.log('frame#onClick start!!!');
      if (this.data.distributed) {
        this.triggerEvent('onBackListener');
      } else {
        wx.navigateBack({
          delta: 0,
        })
      }      
    }
  }
})

组件的使用

组件的引用

在需要使用组件的地方,在json文件里增加:
"frame": "../components/frame/frame"

配置组件

<frame title="XX" visiable="{{true}}" position="0" bind:onBackListener="onClickListener">
	<!-- 写入其它界面配置 -->
</frame>

转载请注明原著地址

  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jjr_1984

谢谢您的鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值