微信小程序自定义组件components(代码详解)


Posted in Javascript onOctober 21, 2019

在写小程序代码的时候,我们发现经常有一段代码我们经常敲,经常使用某一自定义组件,例如商城首页的轮播图和商品详情页的商品展示栏是近乎相同的代码;微信小程序里的弹窗提示可以使用在多个地方…

小程序自定义组件

找到components目录,没有就新建

微信小程序自定义组件components(代码详解)

在compoents目录里新建一个用于存放代码的目录(下面用g-swiper表示)

在g-swiper目录里新建Compoent(名字自取),新建后会和新建Page时一样自动生成四个页面文件(g-swiper.wxml g-swiper.wxss g-swiper.js g-swiper.json)

轮播图实例

<g-swiper list="{{imageList}}" g-class="swiper"/>

在index.wxml里只需要这简短一行代码就能实现一个轮播图组件

微信小程序自定义组件components(代码详解)

json声明

要想使用组件必先声明,在index.json里声明组件名称和地址

{
 "usingComponents": {
  "g-swiper":"/components/g-swiper/g-swiper"
 }
}

在组件的json也必须的声明,g-swiper.json(下面代码直接复制报错请将注释删掉)

{
 "component": true,    // 自定义组件声明
 "usingComponents": {}   // 可选项,用于引用别的组件
}

wxml和wxss

wxml和wxss里的代码跟普通页面里的代码没什么区别

g-swiper.wxml代码

<swiper class="g-class" circular autoplay interval='3000' duration='300' indicator-dots indicator-active-color='#fffff'>
 <block wx:for="{{list}}" wx:key="{{index}}">
  <swiper-item class="swiper-item">
   <image src="{{item}}"/>
  </swiper-item>
 </block>
</swiper>

g-swiper.wxss代码

.swiper-item image{
 width:100%;
 height:100%
}

js

js代码和普通页面js代码有所不同,这里是用Component包起来的而不是被Page包起来的

js代码

Component({
 externalClasses:["g-class"],
 properties: {
   list:{
    type:Array,
    value:[]
   }
 },
})

注意:这里的g-class样式和list数据我将它的定义权利交给引入它的一方,这里是index页面引入它

组件绑定外部方法

组件绑定外部方法的方式,以一自定义button为例

g-btn.wxml代码

<button bindtap="btnTest">g-btn</button>

g-btn.js代码

Component({
 methods: {
   /*
   * 公有方法
   */
  btnTest:function(){
   this.triggerEvent('action')
  }
 }
})

在index里引入并且展示出来

index.wxml代码

<g-btn bind:action="btnTest"></g-btn>

在index.js里加入方法btnTest()

btnTest:function(){
 console.log('g-btn is clicked now!')
}

可以看到Console栏里出现了“g-btn is clicked now!”字样

弹窗组件实例

微信小程序自定义组件components(代码详解)

index页面引入,直接上代码

index.wxml代码

<view class="container">
  <dialog id='dialog' 
   title='这是标题' 
   content='这是对话框的内容' 
   cancelText='取消' 
   confirmText='确定'
   bind:cancelEvent="_cancelEvent" 
   bind:confirmEvent="_confirmEvent">
  </dialog>
  <button type="primary" bindtap="showDialog"> ClickMe! </button>
</view>

index.js代码

Page({
 onReady: function () {
  //获得dialog组件
  this.dialog = this.selectComponent("#dialog");
 },
 showDialog() {
  this.dialog.showDialog();
 },
 //取消事件
 _cancelEvent() {
  console.log('你点击了取消');
  this.dialog.hideDialog();
 },
 //确认事件
 _confirmEvent() {
  console.log('你点击了确定');
  this.dialog.hideDialog();
 }
})

组件dialog目录里

dialog.wxml代码

<view class='wx_dialog_container' hidden="{{!isShow}}">
  <view class='wx-mask'></view>
  <view class='wx-dialog'>
    <view class='wx-dialog-title'>{{ title }}</view>
    <view class='wx-dialog-content'>{{ content }}</view>
    <view class='wx-dialog-footer'>
     <view class='wx-dialog-btn' catchtap='_cancelEvent'>{{ cancelText }}</view>
     <view class='wx-dialog-btn' catchtap='_confirmEvent'>{{ confirmText }}</view>
    </view>
  </view>
</view>

dialog.wxss代码

.wx-mask{
position: fixed;
z-index: 1000;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.3);
}
.wx-dialog{
position: fixed;
z-index: 5000;
width: 80%;
max-width: 600rpx;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background-color: #FFFFFF;
text-align: center;
border-radius: 3px;
overflow: hidden;
}
.wx-dialog-title{
font-size: 18px;
padding: 15px 15px 5px;
}
.wx-dialog-content{
padding: 15px 15px 5px;
min-height: 40px;
font-size: 16px;
line-height: 1.3;
word-wrap: break-word;
word-break: break-all;
color: #999999;
}
.wx-dialog-footer{
display: flex;
align-items: center;
position: relative;
line-height: 45px;
font-size: 17px;
}
.wx-dialog-footer::before{
content: '';
position: absolute;
left: 0;
top: 0;
right: 0;
height: 1px;
border-top: 1px solid #D5D5D6;
color: #D5D5D6;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
}
.wx-dialog-btn{
display: block;
-webkit-flex: 1;
flex: 1;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
position: relative;
}
.wx-dialog-footer .wx-dialog-btn:nth-of-type(1){
color: #353535;
}
.wx-dialog-footer .wx-dialog-btn:nth-of-type(2){
color: #3CC51F;
}
.wx-dialog-footer .wx-dialog-btn:nth-of-type(2):after{
content: " ";
position: absolute;
left: 0;
top: 0;
width: 1px;
bottom: 0;
border-left: 1px solid #D5D5D6;
color: #D5D5D6;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleX(0.5);
transform: scaleX(0.5);
}

dialog.js代码

Component({
 /**
  * 组件的属性列表
  * 用于组件自定义设置
  */
 properties: {
  // 弹窗标题
  title: {      // 属性名
   type: String,   // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
   value: '标题'   // 属性初始值(可选),如果未指定则会根据类型选择一个
  },
  // 弹窗内容
  content: {
   type: String,
   value: '弹窗内容'
  },
  // 弹窗取消按钮文字
  cancelText: {
   type: String,
   value: '取消'
  },
  // 弹窗确认按钮文字
  confirmText: {
   type: String,
   value: '确定'
  }
 },
 /**
  * 私有数据,组件的初始数据
  * 可用于模版渲染
  */
 data: {
  // 弹窗显示控制
  isShow: false
 },
 /**
  * 组件的方法列表
  * 更新属性和数据的方法与更新页面数据的方法类似
  */
 methods: {
  /*
   * 公有方法
   */
  //隐藏弹框
  hideDialog() {
   this.setData({
    isShow: !this.data.isShow
   })
  },
  //展示弹框
  showDialog() {
   this.setData({
    isShow: !this.data.isShow
   })
  },
  /*
  * 内部私有方法建议以下划线开头
  * triggerEvent 用于触发事件
  */
  _cancelEvent() {
   //触发取消回调
   this.triggerEvent("cancelEvent")
  },
  _confirmEvent() {
   //触发成功回调
   this.triggerEvent("confirmEvent");
  }
 }
})

总结

以上所述是小编给大家介绍的微信小程序自定义组件components,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Javascript 相关文章推荐
JavaScript 实现简单的倒计时弹窗DEMO附图
Mar 05 Javascript
jquery获取颜色在ie和ff下的区别示例介绍
Mar 28 Javascript
关于javaScript注册click事件传递参数的不成功问题
Jul 18 Javascript
javascript实现瀑布流自适应遇到的问题及解决方案
Jan 28 Javascript
javascript中JSON对象与JSON字符串相互转换实例
Jul 11 Javascript
JavaScript数据结构与算法之链表
Jan 29 Javascript
浅谈如何实现easyui的datebox格式化
Jun 12 Javascript
基于Javascript实现文件实时加载进度的方法
Oct 12 Javascript
Vue 技巧之控制父类的 slot
Feb 24 Javascript
js实现动态时钟
Mar 12 Javascript
详解vue-router的Import异步加载模块问题的解决方案
May 13 Javascript
three.js着色器材质的内置变量示例详解
Aug 16 Javascript
微信小程序实现侧边分类栏
Oct 21 #Javascript
微信小程序实现侧边栏分类
Oct 21 #Javascript
微信小程序中weui用法解析
Oct 21 #Javascript
Vue使用vue-recoure + http-proxy-middleware + vuex配合promise实现基本的跨域请求封装
Oct 21 #Javascript
小程序实现日历左右滑动效果
Oct 21 #Javascript
微信小程序可滑动月日历组件使用详解
Oct 21 #Javascript
微信小程序可滑动周日历组件使用详解
Oct 21 #Javascript
You might like
不用数据库的多用户文件自由上传投票系统(2)
2006/10/09 PHP
PHP使用递归生成文章树
2015/04/21 PHP
PHP之十六个魔术方法详细介绍
2016/11/01 PHP
微信公众平台开发教程④ ThinkPHP框架下微信支付功能图文详解
2019/04/10 PHP
使用jquery与图片美化checkbox和radio控件的代码(打包下载)
2010/11/11 Javascript
Package.js  现代化的JavaScript项目make工具
2012/05/23 Javascript
jQuery中queue()方法用法实例
2014/12/29 Javascript
在父页面得到zTree已选中的节点的方法
2015/02/12 Javascript
input点击后placeholder中的提示消息消失
2016/01/15 Javascript
Bootstrap按钮组件详解
2016/04/26 Javascript
JS搜狐面试题分析
2016/12/16 Javascript
Angularjs中的ui-bootstrap的使用教程
2017/02/19 Javascript
BootStrap Select清除选中的状态恢复默认状态
2017/06/20 Javascript
js实现方块上下左右移动效果
2017/08/17 Javascript
Angularjs 手写日历的实现代码(不用插件)
2017/10/18 Javascript
webpack本地开发环境无法用IP访问的解决方法
2018/03/20 Javascript
[48:31]完美世界DOTA2联赛PWL S3 DLG vs Phoenix 第二场 12.17
2020/12/19 DOTA
python文件操作整理汇总
2014/10/21 Python
在Python中使用SimpleParse模块进行解析的教程
2015/04/11 Python
Python实现的括号匹配判断功能示例
2018/08/25 Python
解析Python的缩进规则的使用
2019/01/16 Python
Keras:Unet网络实现多类语义分割方式
2020/06/11 Python
利用python清除移动硬盘中的临时文件
2020/10/28 Python
python语言实现贪吃蛇游戏
2020/11/13 Python
python性能测试工具locust的使用
2020/12/28 Python
CSS3中各种颜色属性的使用教程
2016/05/17 HTML / CSS
Speedo速比涛德国官方网站:世界领先的泳装品牌
2019/08/26 全球购物
IBatis持久层技术
2016/07/18 面试题
体育教师工作总结的自我评价
2013/10/10 职场文书
金融专业推荐信
2013/11/14 职场文书
临床护理求职信
2014/04/26 职场文书
2014年小学语文工作总结
2014/12/20 职场文书
公司经营目标责任书
2015/01/29 职场文书
2015年环卫工作总结
2015/04/28 职场文书
世界名著读书笔记
2015/06/25 职场文书
2015年重阳节主持词
2015/07/04 职场文书