手把手教你实现微信小程序中的自定弹窗

前言

之前做小程序开发的时候,对于开发来说比较头疼的莫过于自定义组件了,当时官方对这方面的文档也只是寥寥几句,一笔带过而已,所以写起来真的是非常非常痛苦!!

好在微信小程序的库从 1.6.3 开始,官方对于自定义组件这一块有了比较大的变动,首先比较明显的感觉就是文档比以前全多了,有木有!(小程序文档),现在小程序支持简洁的组件化编程,可以将页面内的功能模块抽象成自定义组件,以便在不同的页面中复用,提高自己代码的可读性,降低自己维护代码的成本!

本篇文章就是手把手教你实现小程序中自定义组件,坐稳啦~

具体实现

要做自定义组件,我们先定一个小目标,比如说我们在小程序中实现一下 WEUI 中的弹窗组件,基本效果图如下。

WEUI弹框

 

Step1

我们初始化一个小程序(本示例基础版本库为 1.7 ),删掉里面的示例代码,并新建一个 components 文件夹,用于存放我们以后开发中的所用组件,今天我们的目的是实现一个 弹框 组件,因此,我们在 components 组件中新建一个 Dialog 文件夹来存放我们的弹窗组件,在 Dialog 下右击新建 Component 并命名为 dialog 后,会生成对应的 json wxml wxss js 4个文件,也就是一个自定义组件的组成部分,此时你的项目结构应该如下图所示:

项目结构

 

Step2

组件初始化工作准备完成,接下来就是组件的相关配置,首先我们需要声明自定义组件,也就是将 dialog.json 中 component 字段设为 true :

 
  1. {

  2. "component": true, // 自定义组件声明

  3. "usingComponents": {} // 可选项,用于引用别的组件

  4. }

其次,我们需要在 dialog.wxml 文件中编写弹窗组件模版,在 dialog.wxss 文件中加入弹窗组件样式,它们的写法与页面的写法类似,我就不赘述,直接贴代码啦~

dialog.wxml 文件如下:

 
  1. <!--components/Dialog/dialog.wxml-->

  2. <view class='wx_dialog_container' hidden="{{!isShow}}">

  3. <view class='wx-mask'></view>

  4. <view class='wx-dialog'>

  5. <view class='wx-dialog-title'>{{ title }}</view>

  6. <view class='wx-dialog-content'>{{ content }}</view>

  7. <view class='wx-dialog-footer'>

  8. <view class='wx-dialog-btn' catchtap='_cancelEvent'>{{ cancelText }}</view>

  9. <view class='wx-dialog-btn' catchtap='_confirmEvent'>{{ confirmText }}</view>

  10. </view>

  11. </view>

  12. </view>

dialog.wxss 文件如下:

 
  1. /* components/Dialog/dialog.wxss */

  2. .wx-mask{

  3. position: fixed;

  4. z-index: 1000;

  5. top: 0;

  6. right: 0;

  7. left: 0;

  8. bottom: 0;

  9. background: rgba(0, 0, 0, 0.3);

  10. }

  11. .wx-dialog{

  12. position: fixed;

  13. z-index: 5000;

  14. width: 80%;

  15. max-width: 600rpx;

  16. top: 50%;

  17. left: 50%;

  18. -webkit-transform: translate(-50%, -50%);

  19. transform: translate(-50%, -50%);

  20. background-color: #FFFFFF;

  21. text-align: center;

  22. border-radius: 3px;

  23. overflow: hidden;

  24. }

  25. .wx-dialog-title{

  26. font-size: 18px;

  27. padding: 15px 15px 5px;

  28. }

  29. .wx-dialog-content{

  30. padding: 15px 15px 5px;

  31. min-height: 40px;

  32. font-size: 16px;

  33. line-height: 1.3;

  34. word-wrap: break-word;

  35. word-break: break-all;

  36. color: #999999;

  37. }

  38. .wx-dialog-footer{

  39. display: flex;

  40. align-items: center;

  41. position: relative;

  42. line-height: 45px;

  43. font-size: 17px;

  44. }

  45. .wx-dialog-footer::before{

  46. content: '';

  47. position: absolute;

  48. left: 0;

  49. top: 0;

  50. right: 0;

  51. height: 1px;

  52. border-top: 1px solid #D5D5D6;

  53. color: #D5D5D6;

  54. -webkit-transform-origin: 0 0;

  55. transform-origin: 0 0;

  56. -webkit-transform: scaleY(0.5);

  57. transform: scaleY(0.5);

  58. }

  59. .wx-dialog-btn{

  60. display: block;

  61. -webkit-flex: 1;

  62. flex: 1;

  63. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);

  64. position: relative;

  65. }

  66. .wx-dialog-footer .wx-dialog-btn:nth-of-type(1){

  67. color: #353535;

  68. }

  69. .wx-dialog-footer .wx-dialog-btn:nth-of-type(2){

  70. color: #3CC51F;

  71. }

  72. .wx-dialog-footer .wx-dialog-btn:nth-of-type(2):after{

  73. content: " ";

  74. position: absolute;

  75. left: 0;

  76. top: 0;

  77. width: 1px;

  78. bottom: 0;

  79. border-left: 1px solid #D5D5D6;

  80. color: #D5D5D6;

  81. -webkit-transform-origin: 0 0;

  82. transform-origin: 0 0;

  83. -webkit-transform: scaleX(0.5);

  84. transform: scaleX(0.5);

  85. }

step3

组件的结构和样式都有了,还缺少什么呢,没错,还缺 js , 眼睛比较犀利的同学,可能已经发现了我们在 dialog.wxml 文件中的会有一些比如 {{ isShow }} 、{{ title }} 这样的模版变量,还定义了 _cancelEvent 和 _confirmEvent 两个方法,其具体实现就是在 dialog.js 中。

dialog.js 是自定义组件的构造器,是使用小程序中 Component 构造器生成的,调用 Component 构造器时可以用来指定自定义组件的属性、数据、方法等,具体的细节可以参考一下官方的文档

下面我通过代码注释解释一下构造器中的一些属性的使用:

 
  1. // components/Dialog/dialog.js

  2. Component({

  3. options: {

  4. multipleSlots: true // 在组件定义时的选项中启用多slot支持

  5. },

  6. /**

  7. * 组件的属性列表

  8. * 用于组件自定义设置

  9. */

  10. properties: {

  11. // 弹窗标题

  12. title: { // 属性名

  13. type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)

  14. value: '标题' // 属性初始值(可选),如果未指定则会根据类型选择一个

  15. },

  16. // 弹窗内容

  17. content :{

  18. type : String ,

  19. value : '弹窗内容'

  20. },

  21. // 弹窗取消按钮文字

  22. cancelText :{

  23. type : String ,

  24. value : '取消'

  25. },

  26. // 弹窗确认按钮文字

  27. confirmText :{

  28. type : String ,

  29. value : '确定'

  30. }

  31. },

  32.  
  33. /**

  34. * 私有数据,组件的初始数据

  35. * 可用于模版渲染

  36. */

  37. data: {

  38. // 弹窗显示控制

  39. isShow:false

  40. },

  41.  
  42. /**

  43. * 组件的方法列表

  44. * 更新属性和数据的方法与更新页面数据的方法类似

  45. */

  46. methods: {

  47. /*

  48. * 公有方法

  49. */

  50.  
  51. //隐藏弹框

  52. hideDialog(){

  53. this.setData({

  54. isShow: !this.data.isShow

  55. })

  56. },

  57. //展示弹框

  58. showDialog(){

  59. this.setData({

  60. isShow: !this.data.isShow

  61. })

  62. },

  63. /*

  64. * 内部私有方法建议以下划线开头

  65. * triggerEvent 用于触发事件

  66. */

  67. _cancelEvent(){

  68. //触发取消回调

  69. this.triggerEvent("cancelEvent")

  70. },

  71. _confirmEvent(){

  72. //触发成功回调

  73. this.triggerEvent("confirmEvent");

  74. }

  75. }

  76. })

step4

截至目前为止,你应该完成了一个自定义弹窗组件的大部分,可是你保存后并没有发现任何变化,因为我们还需要在 index.wxml 文件中引入它!

首先需要在 index.json 中引入组件:

 
  1. {

  2. "usingComponents": {

  3. "dialog": "/components/Dialog/dialog"

  4. }

  5. }

然后我们在 index.wxml 中引入它,并增加我们自定义的一些值,如下

 
  1. <!--index.wxml-->

  2. <view class="container">

  3. <dialog id='dialog'

  4. title='我是标题'

  5. content='恭喜你,学会了小程序组件'

  6. cancelText='知道了'

  7. confirm='谢谢你'

  8. bind:cancelEvent="_cancelEvent"

  9. bind:confirmEvent="_confirmEvent">

  10. </dialog>

  11.  
  12. <button type="primary" bindtap="showDialog"> ClickMe! </button>

  13. </view>

嗯哪,还差最后一步,index.js 配置,没错,这个也很简单,我就复制粘贴了

 
  1. //index.js

  2. //获取应用实例

  3. const app = getApp()

  4.  
  5. Page({

  6.  
  7. /**

  8. * 生命周期函数--监听页面初次渲染完成

  9. */

  10. onReady: function () {

  11. //获得dialog组件

  12. this.dialog = this.selectComponent("#dialog");

  13. },

  14.  
  15. showDialog(){

  16. this.dialog.showDialog();

  17. },

  18.  
  19. //取消事件

  20. _cancelEvent(){

  21. console.log('你点击了取消');

  22. this.dialog.hideDialog();

  23. },

  24. //确认事件

  25. _confirmEvent(){

  26. console.log('你点击了确定');

  27. this.dialog.hideDialog();

  28. }

  29.  
  30. })

到此!大功告成!

step5

让我们测试一下试试看:

初始化

 

点击按钮之后呢,会出现如下效果:

弹窗出现

 

点击取消或者确定按钮的话,我们在事件中设置了弹窗会关闭,并会打印出相应的信息,具体点击完应该怎么做,就看你们自己发挥了,我只能帮你到这里了~

点击取消

 

点击确定

 

总结

现在,你已经基本掌握了小程序中的自定义组件开发技巧,怎么样,是不是很棒,应该给自己点个赞,打个call。 总体来说,小程序推出自定义组件后,感觉方便了很多,还没有 get 的小伙伴们,赶紧学习学习,以后多用组件化开发,就不会那么难受了,加油哦~

具体代码,我已经托管到 github 上了,欢迎 issue~

popup 组件的示例代码里

 
  1. showPopup() {

  2. let popupComponent = this.selectComponent('.J_Popup')

  3. popupComponent && popupComponent.show()

  4. },

selectComponent 方法不是Component构造器内部使用的方法吗,在 Page 里能使用吗?

经过验证是可以在page中调用selectComponent方法得到一个组件的实例,对于popup、dialog等组件,通过selectComponent获取组件实例后,可直接调用该组件的show()、hide()方法。

猜你喜欢

转载自blog.csdn.net/sd19871122/article/details/82117002